2
class A
{
private:
   int a;

public:
   int get_a()
   {
      return a;
   }
   A(int mode)
   {
      a = 0;
   }
   A()
   {
      a = 5;
   }
};

class B
{
public:
   A b(0);   
};

class C
{
   int c;

public:
   C(int mode)
   {
      c = 0;
   }
   C()
   {
      c = 1;
   }
};

int main()
{
   B bb;
   C cc(0);
   //cout << bb.b.get_a();
   system("pause");
   return 0;
}

if im using () brackets on b in class B it gives the error if i switch to {} everything is fine . My question is shouldn't i be allowed to do that since on cc in main it doesn't give any error. And im allowed to use () brackets when initializing objects.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
luka_bur3k
  • 367
  • 1
  • 2
  • 11

1 Answers1

5

According to the C++ 20 Standard (11.4 Class members) you may use a brace-or-equal-initializer to initialize a data member of a class

member-declarator:
    ...
    declarator brace-or-equal-initializeropt

So you may use either

class B
{
public:
   A b = 0;   
};

or

class B
{
public:
   A b { 0 };   
};

This allows to avoid an ambiguity with a function declaration.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335