class A
{
public:
A(const int n_);
A(const A& that_);
A& operator=(const A& that_);
};
A::A(const int n_)
{ cout << "A::A(int), n_=" << n_ << endl; }
A::A(const A& that_) // This is line 21
{ cout << "A::A(const A&)" << endl; }
A& A::operator=(const A& that_)
{ cout << "A::operator=(const A&)" << endl; }
int foo(const A& a_)
{ return 20; }
int main()
{
A a(foo(A(10))); // This is line 38
return 0;
}
このコードを実行すると、o/pが得られます。
A :: A(int)、n_ = 10
A :: A(int)、n_ = 20
どうやら、コピーコンストラクタは決して呼び出されません。
class A
{
public:
A(const int n_);
A& operator=(const A& that_);
private:
A(const A& that_);
};
ただし、プライベートにすると、次のコンパイルエラーが発生します。
Test.cpp:関数内'int main()':
Test.cpp:21:エラー:'A :: A(const A&)'はプライベート
ですTest.cpp:38:エラー:このコンテキスト内
コンパイラが実際にコピーコンストラクタを使用していないのに、なぜ文句を言うのですか?
gccバージョン4.1.220070925(Red Hat 4.1.2-33)を使用しています