简体   繁体   中英

Implicit copy constructor and inheritance

I know this kind of question has been asked many times, and I've read different answers about it, as well as some parts of the ISO standard.
But still, I need a few clarifications about the exact behaviour expected by the C++ standard.

Assuming this:

class A
{
    public:

        A( void ) {}
        A( const A & a ) {}
};

class B: public A
{
    public:

        B( void ) {}
        B( const B & b ) {}
};

I know that calling the copy constructor of the B class won't call the copy constructor of the A class, and that we can use an initialisation list in order to do it properly.

I also know about using using in order to inherit constructors.

But what does the standard exactly mandates about a derived class which does not provides explicitly a copy constructor, while the base class does:

class A
{
    public:

        A( void ) {}
        A( const A & a ) {}
};

class B: public A
{};

I always thought the compiler would implicitly define a copy constructor for class B , thus hiding the copy constructor of class A , which won't be called.

However, looks like it's not the case, and the the A copy constructor is called.
Compiling with Clang on OS X 10.10.

So is this something mandatory, or something that can be implementation defined, meaning we should not rely on this behaviour?

In the C++ standard, I've found the following, but it's clearly not crystal-clear to me:

An inheriting constructor for a class is implicitly defined when it is odr-used (3.2) to create an object of its class type (1.8). An implicitly-defined inheriting constructor performs the set of initializations of the class that would be performed by a user-written inline constructor for that class with a mem-initializer-list whose only mem-initializer has a mem-initializer-id that names the base class denoted in the nested-name-specifier of the using-declaration and an expression-list as specified below, and where the compound-statement in its function body is empty (12.6.2).

I would really enjoy a clarification on it, in accordance with the standard, and also with multiple inheritance in mind.

From http://en.cppreference.com/w/cpp/language/copy_constructor (emphasis mine):

Implicitly-defined copy constructor

If the implicitly-declared copy constructor is neither deleted nor trivial, it is defined (that is, a function body is generated and compiled) by the compiler if odr-used. For union types, the implicitly-defined copy constructor copies the object representation (as by std::memmove). For non-union class types (class and struct), the constructor performs full member-wise copy of the object's bases and non-static members , in their initialization order, using direct initialization.

So it seems that compiler generated copy constructor will call the base copy constructor just as it calls the copy constructors of the members.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM