#include <iostream>
using namespace std;
class A
{
public:
int x;
A(){x= 30; cout << "A's constructor called " << endl;}
A(const A& obj) { cout<<"A's copy constructor called " <<endl; }
};
class B
{
public:
static A sample;
B() { cout << "B's constructor called " << endl; }
static A getSample() { return sample; }
};
A B::sample;
int main()
{
A one;
A two = B::getSample();
cout<<B::sample.x<<endl;
cout<<one.x<<endl;
cout<<two.x<<endl;
return 0;
}
출력 위의 코드 :C++ 복사 건설
A's constructor called
A's constructor called
A's copy constructor called
30
30
-1032819680
왜 B::getSample()
에 X의 생성자 복사 값을 복사하지 않습니다. 즉, B::getSample()=30
인데 왜 two.x
이 -1032xxxxxx
입니까?