skip to Main Content
class AAA
{
    int m_Int;
public:
    AAA() : m_Int{12} {}
};

class BBB
{
    int m_Int1;
public:
    BBB() : m_Int1{12} {}
};

class CCC : public AAA, public BBB {};
AAA a;
BBB b;

CCC c{ a, b };

Why can object c be constructed by parent class object?

I tried to find out which standard support this syntax. I wrote the code with Visual Studio, and I found C++ 14 does not support this, but C++17 does. I also found that the construct process of c call AAA and BBB‘s copy constructor.

I want to know what the syntax is and where to find the item.

2

Answers


  1. This is AggregateType initialization, since C++17 you can have an AggregateType even if it inherits from other classes (provided inheritance is non-virtul public and all base classes and the body of derived conform the requirements listed by standard on AggregateType).

    Login or Signup to reply.
  2. This is aggregate initialization.

    Since C++17, CCC is an aggregate, where one of the requirements was relaxed from "no base classes" to "no virtual, private, or protected base classes".

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search