3
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.

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39
Sheperd
  • 41
  • 5
  • See dupe: [Why does having a base class disqualify a class from being aggregate?](https://stackoverflow.com/a/68501053/12002570). Also, see [dupe2](https://stackoverflow.com/a/53678576/12002570) and [dupe3](https://stackoverflow.com/a/48402461/12002570) – Jason Nov 25 '22 at 13:38

2 Answers2

3

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).

lorro
  • 10,687
  • 23
  • 36
3

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".

Caleth
  • 52,200
  • 2
  • 44
  • 75