ABase( const ABase& );
The copy constructor becomes private, so a copy of the class object cannot be created using this private copy constructor, which leads to an error.
A a = A(); // line 1
Uses A::A(const A&) to create a new object A A obtained from ABase , and it calls ABase::ABase(const ABase&) in its constructor, which is private to it, will not compile either.
Below is the output on Ideone. It does not compile even on gcc.
Why does this work on Visual Studio?
The reason is a possible Optimization of the return value by the visual C ++ compiler, which returns the copy constructor.
According to the C ++ 12.8 standard, copying class class objects
When certain criteria are met, implementations are allowed to omit the copy construction of the class object, even if the copy constructor and / or destructor for the object have side effects. In such cases, the implementation considers the source and purpose of the omitted copy operation as just two different ways of accessing the same object, and the destruction of this object occurs at later times when two objects would be destroyed without optimization .111)
See my answer here , which refers to standard and sample code in this regard.
A a2( a ); // line 2
It does not compile for the same reason that ABase::ABase(const ABase&) is closed.
B b = B(); // line 3
It does not compile because B( const B& ); is closed.
Alok save
source share