Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

private copy constructor/assignment operator and copy-initialization

Tags:

c++

boost

This is a followup of this question

in the following code, why does line 1 compiles while line 2 and 3 does not (using visual C++ 2010)

class ABase
{
protected:
    ABase() {}
    ~ABase() {}
private:
    ABase( const ABase& );
    const ABase& operator=( const ABase& );
};

class A : ABase
{
};

class B
{
public:
    B() {}
    ~B() {}
private:
    B( const B& );
    const B& operator=( const B& );
};

int main( void )
{
    A a = A(); // line 1
    A a2( a ); // line 2
    B b = B(); // line 3

    return 0;
}

(note BA is a copy of boost::noncopyable)

edit: My problem is not to know why line 2 and 3 does not compile (I know that, the copy constructor is private), but why line 1 does.

like image 527
foke Avatar asked Jun 29 '11 16:06

foke


People also ask

What is the difference between copy constructor and copy assignment operator?

The difference between a copy constructor and an assignment operator is that a copy constructor helps to create a copy of an already existing object without altering the original value of the created object, whereas an assignment operator helps to assign a new value to a data member or an object in the program.

What is assignment and copy initialization?

Direct Initialization or Assignment Operator (Syntax) This assigns the value of one object to another object both of which are already exists. Copy initialization is used when a new object is created with some existing object. This is used when we want to assign existing object to new object.

What is difference between copy constructor and constructor?

Constructor: It is a method which has the same name as the class which is used to create an instance of the class. Copy Constructor: Used to create an object by copying variables from another object of the same class. The main purpose is to create a new object from an existing one by copying variables.


2 Answers

Indeed line 1 should not compile.

Apparently vc++2010 has problems enforcing language rules in this case (may be because they're related to a base class and not to the object itself).

g++ diagnostic message about line 1 is very clear

ncopy.cpp: In copy constructor ‘A::A(const A&)’:
ncopy.cpp:7: error: ‘ABase::ABase(const ABase&)’ is private
ncopy.cpp:12: error: within this context
ncopy.cpp: In function ‘int main()’:
ncopy.cpp:27: note: synthesized method ‘A::A(const A&)’ first required here 
like image 163
6502 Avatar answered Oct 05 '22 23:10

6502


The compiler is wrong in accepting the first use. Even if the copy is elided, the copy constructor must be accessible for the code to be correct.

In this particular case there is an implicitly declared copy constructor in A:

§12.8/4 If the class definition does not explicitly declare a copy constructor, one is declared implicitly.

That is implicitly defined:

§12.8/7 An implicitly-declared copy constructor is implicitly defined if it is used to initialize an object of its class type from a copy of an object of its class type or of a class type derived from its class type108). [Note: the copy constructor is implicitly defined even if the implementation elided its use (12.2). ] A program is ill-formed if the class for which a copy constructor is implicitly defined has:

— a nonstatic data member of class type (or array thereof) with an inaccessible or ambiguous copy constructor, or

— a base class with an inaccessible or ambiguous copy constructor.

like image 31
David Rodríguez - dribeas Avatar answered Oct 06 '22 01:10

David Rodríguez - dribeas