#ifndef INTVECTOR_H
#define INTVECTOR_H
using namespace std;
class IntVector{
private:
unsigned sz;
unsigned cap;
int *data;
public:
IntVector();
IntVector(unsigned size);
IntVector(unsigned size, int value);
unsigned size() const;
};
#endif
體
#include "IntVector.h"
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
IntVector::IntVector(){
sz = 0;
cap = 0;
data = NULL;
}
IntVector::IntVector(unsigned size){
sz = size;
cap = size;
data = new int[sz];
*data = 0;
}
IntVector::IntVector(unsigned size, int value){
sz = size;
cap = size;
data = new int[sz];
for(unsigned int i = 0; i < sz; i++){
data[i] = value;
}
}
unsigned IntVector::size() const{
return sz;
}
當我在主測試我的功能,(intVector的(6,4); COUT < < testing.size()< < ENDL;),我的當我在IntVector函數中分配sz和cap時,testing.size()測試在理論上應該是6時始終輸出0。任何想法,爲什麼它輸出0?
如果main()是這樣的:'IntVector(6,4);',我想知道'testing'在哪裏出現。 – WhozCraig