#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); < < testing.size() < < ENDL cout을) 내를 testing.size() 테스트는 IntVector 함수에서 sz와 cap을 지정하면 이론적으로 6 일 때 일관되게 0을 출력합니다. 왜 0을 출력하는지에 대한 아이디어가 있습니까? 여기에 폐기되는 임시을 만드는 것처럼
이것이'main()'에있는 경우 :'IntVector (6, 4);','testing'이 어디로 들어 왔는지 알고 싶습니다. – WhozCraig