should this program compile?

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
 
Y

Yang Zhang

Yang said:
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

I've also tried the Intel compiler (version 8.1). It compiled, but gave
a warning like this:
constructor.cc(27): warning #734: "A::A(const A &)", required for copy
that was eliminated, is inaccessible
A a = 3 ;
^
Also I've found that the Comeau C/C++ compiler (4.3.3 for online
evaluation) gave a better explanation of the problem:

"ComeauTest.c", line 27: error: "A::A(const A &)" is inaccessible
(Even though the copy was eliminated, the standard
still requires it to be accessible)
A a = 3 ;
^
But can someone explain what happened when executing "A a = 3 ;" and the
rationale for requiring the copy constructor being public?

Thanks!
--Yang
 
K

Karl Heinz Buchegger

Yang said:
A a = 3 ;
^
But can someone explain what happened when executing "A a = 3 ;" and the
rationale for requiring the copy constructor being public?

A a = 3;

is another form of

A a( A(3) );

That is: create an object of type A and initialize it with 3 (using
a construcor that takes an int argument). Then use that temporary object
in conjunction with the copy constructor to create the actual object 'a'.

As compilers have told you: It is allowed to eliminate that temporary
object and thus the usage of the copy constructor and directly initialize
the 'a' object with 3, but nevertheless the compiler must do 'as if'. Hence
the copy constructor must be accessible.

Use:
A a( 3 );
 
S

Sharad Kala

Yang Zhang said:
Also I've found that the Comeau C/C++ compiler (4.3.3 for online
evaluation) gave a better explanation of the problem:

"ComeauTest.c", line 27: error: "A::A(const A &)" is inaccessible
(Even though the copy was eliminated, the standard
still requires it to be accessible)
A a = 3 ;
^
But can someone explain what happened when executing "A a = 3 ;" and the
rationale for requiring the copy constructor being public?

First a temporary object is being created. Then *ideally* copy constructor
gets invoked. Standard gives the relaxation to elide the copy construction
step as an optimization. However it gives this relaxation only if the copy
constructor is accessible.

Sharad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top