나는 질문했습니다 this. 제 질문은 지금 입니다 어떻게이 작동합니까? 좀 더 자세히 설명하기 위해 아직 초기화되지 않은 객체를 어떻게 가리킬 수 있습니까? 나는이 MWE를 만들었고 그 객체가 할당 된 사본이 아니라 복사 된 것임을 보여줍니다. 객체는 아직 초기화되지 않았습니다. 그러나 객체를 가리킬 수 있습니다.아직 초기화되지 않은 회원을 가리키고 있습니다
#include <iostream>
class Foo {
public:
int x;
Foo(const Foo& ori_foo) {
std::cout << "constructor" << std::endl;
x = ori_foo.x;
}
Foo& operator = (const Foo& ori_foo) {
std::cout << "operator =" << std::endl;
x = ori_foo.x;
return *this;
}
Foo(int new_x) {
x = new_x;
}
};
class BarParent {
public:
Foo *p_foo;
BarParent(Foo* new_p_foo) : p_foo(new_p_foo)
{
std::cout << (*new_p_foo).x << std::endl;
}
};
class BarChild : public BarParent {
public:
Foo foo;
BarChild(Foo new_foo)
:BarParent(&foo) //pointer to member not yet initialised
,foo(new_foo) // order of initilization POINT OF INTEREST
{}
};
int main() {
Foo foo(101);
BarChild bar(foo);
std::cout << bar.p_foo->x << std::endl;
std::cout << bar.foo.x << std::endl;
}
출력 :
constructor
0
constructor
101
101
메모리 처리하는 방법의 세부 사항에 점점 두려워하지 마십시오. 그리고, 모든 회원이 거주하는 곳. 이 라인
BarChild bar(foo);
BarChild
개체에 대한 컴파일러 보유 충분한 스택 공간에서
아직 명확하지 않은 경우 알려주십시오 – aiao
죄송합니다. 실수로 편집되었습니다. : BarParent (& foo) – aiao
BarParent 생성자에서 Foo 값을 인쇄 해 보았습니까? – nonsensickle