c++inheritancec++11default-constructorinherited-constructors

Need an example showing that default constructor is not inherited


I know that default constructor is not inherited, as stated in n3337.

And there is an example there:

struct B2 {
  B2(int = 13, int = 42);
};

struct D2 : B2 {
  using B2::B2;
};

With quite good explanation:

The candidate set of inherited constructors in D2 for B2 is

...
—B2(int = 13, int = 42)
—B2(int = 13)
—B2()

And most important:

The set of constructors present in D2 is
—D2(), implicitly-declared default constructor, not inherited

For me this example does not show the difference, in a sense that even if this very constructor was inherited - its behavior was not different from the implicitly-declared default constructor.

I need an example showing the difference in the way that can be easily understand for, let say, an audience familiar with C++03 but wanting to learn C++11.


[UPDATE]
All answers (including my own) are of kind "if default c-tor was inherited then the example would compile/not compile".

I'd prefer answers where the outcome (observable behavior) is different than it would be otherwise.


Solution

  • Consider:

    struct foo
    {
        foo() {}
        foo(int) {}
    };
    
    struct bar : foo
    {
        using foo::foo;
    };
    
    int main()
    {
        bar b;
    }
    

    This compiles: Since bar has no user-declared constructors, a default constructor will be declared implicitly.

    struct foo
    {
        foo() {}
        foo(int) {}
    };
    
    struct bar : foo
    {
        using foo::foo;
        bar(double) {}
    };
    
    int main()
    {
        bar b;
    }
    

    This does not compile. The default constructor is not inherited, and it is not declared implicitly, since there is the bar(double) constructor.