should this program compile?

Discussion in 'C++' started by Yang Zhang, Sep 20, 2005.

  1. Yang Zhang

    Yang Zhang Guest

    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
     
    Yang Zhang, Sep 20, 2005
    #1
    1. Advertising

  2. Yang Zhang

    Yang Zhang Guest

    Yang Zhang wrote:
    > 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
     
    Yang Zhang, Sep 20, 2005
    #2
    1. Advertising

  3. Yang Zhang wrote:
    >
    > 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 );

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 20, 2005
    #3
  4. Yang Zhang

    Sharad Kala Guest

    "Yang Zhang" <> wrote in message
    [snip]
    > 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
     
    Sharad Kala, Sep 20, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    460
    mlimber
    Oct 12, 2005
  2. Howard Gardner
    Replies:
    6
    Views:
    319
    Howard Gardner
    Jul 21, 2006
  3. Nagaraj
    Replies:
    1
    Views:
    893
    Lionel B
    Mar 1, 2007
  4. ~~~ .NET Ed ~~~

    How should control images should be handled?

    ~~~ .NET Ed ~~~, Oct 31, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    254
    John Saunders
    Nov 3, 2004
  5. Josef 'Jupp' SCHUGT

    What the FAQs should and should not contain

    Josef 'Jupp' SCHUGT, Aug 19, 2005, in forum: Ruby
    Replies:
    0
    Views:
    206
    Josef 'Jupp' SCHUGT
    Aug 19, 2005
Loading...

Share This Page