Y
Yang Zhang
Here is a program:
/////////////////////////////////////////////////
#include <iostream>
using namespace std ;
class A {
int a ;
A(const A& aA) {
a=aA.a ;
cout<<"copy constructor called!"<<endl ;
}
public:
A(int i=0):a(i) {
cout<<"int constructor called!"<<endl ;
}
A& operator=(int i) {
a=i ;
cout<<"int assignment called!"<<endl ;
return *this ;
}
A& operator=(const A& aA) {
a=aA.a ;
cout<<"assignment operator called!"<<endl ;
return *this ;
}
} ;
int main() {
A a = 3 ;
}
/////////////////////////////////////////////////
Should it compile? Or in other words, is it a correct C++ program?
Initially I thought that "A a = 3 ;" just requires the default
constructor, which is available publicly. But GNU g++ (version 4.0.0)
will not compile the code, it complained:
=========================================
constructor.cc: In function `int main()':
constructor.cc:6: error: 'A::A(const A&)' is private
constructor.cc:27: error: within this context
constructor.cc:6: error: 'A::A(const A&)' is private
constructor.cc:27: error: within this context
constructor.cc:27: error: initializing temporary from result of
'A::A(int)'
=========================================
Sun's C++ compiler (Sun WorkShop 6 update 2 C++ 5.3 2001/05/15) will not
compile the code either. It complained something similar:
=========================================
"constructor.cc", line 27: Error: A::A(const A&) is not accessible from
main().
1 Error(s) detected.
=========================================
However, SGI's C++ compiler (MIPSpro Compilers: Version 7.3.1.3m) would
just compile the code without problem.
This is actually a real problem occurred in my work. I just simplified
it to this program. I am not sure whether such kind of initialization
will require the copy constructor being public. Any comments on this?
Thanks!
Regards,
--Yang
/////////////////////////////////////////////////
#include <iostream>
using namespace std ;
class A {
int a ;
A(const A& aA) {
a=aA.a ;
cout<<"copy constructor called!"<<endl ;
}
public:
A(int i=0):a(i) {
cout<<"int constructor called!"<<endl ;
}
A& operator=(int i) {
a=i ;
cout<<"int assignment called!"<<endl ;
return *this ;
}
A& operator=(const A& aA) {
a=aA.a ;
cout<<"assignment operator called!"<<endl ;
return *this ;
}
} ;
int main() {
A a = 3 ;
}
/////////////////////////////////////////////////
Should it compile? Or in other words, is it a correct C++ program?
Initially I thought that "A a = 3 ;" just requires the default
constructor, which is available publicly. But GNU g++ (version 4.0.0)
will not compile the code, it complained:
=========================================
constructor.cc: In function `int main()':
constructor.cc:6: error: 'A::A(const A&)' is private
constructor.cc:27: error: within this context
constructor.cc:6: error: 'A::A(const A&)' is private
constructor.cc:27: error: within this context
constructor.cc:27: error: initializing temporary from result of
'A::A(int)'
=========================================
Sun's C++ compiler (Sun WorkShop 6 update 2 C++ 5.3 2001/05/15) will not
compile the code either. It complained something similar:
=========================================
"constructor.cc", line 27: Error: A::A(const A&) is not accessible from
main().
1 Error(s) detected.
=========================================
However, SGI's C++ compiler (MIPSpro Compilers: Version 7.3.1.3m) would
just compile the code without problem.
This is actually a real problem occurred in my work. I just simplified
it to this program. I am not sure whether such kind of initialization
will require the copy constructor being public. Any comments on this?
Thanks!
Regards,
--Yang