class example1
{
private:
int i;
public:
example1(){i = 1;}
int getI(){return i;}
};
class example2
{
public:
example2(){}
vector<example2> this_vector_wont_compile(3);
vector <example2> theVec;
void addVec()
{
//what's the scope of this?
//does push_back create a pointer
//to which a deep copy of the example1 instance
//returned by the constructor is performed?
theVec.push_back(example2());
}
};
int main()
{
example2 theExample;
theExample.theVec[0]; //can be accessed, instance of example1 in scope.
return 0;
}
안녕하세요, std :: vectors를 사용하여 기본 메모리 작업을 이해하려고합니다. 위 예제는 과거에 어떻게 사용되었는지를 묻지 않고 어떻게 사용했는지를 보여줍니다.C++ std 벡터 내용 범위
example2() 생성자는 addVec() 함수가 끝날 때 범위를 벗어나는 인스턴스를 반환하므로 theVec은 theVec이 범위 내에있는 동안 어떻게 추가합니까?
또한 클래스에서 상수 크기를 갖는 것으로서 std :: vector를 선언하면 컴파일러 오류가 발생하며 어떻게 피할 수 있습니까?
감사합니다. pmr 및 @obmarg, 내 질문에 완벽하게 대답합니다. –