0

anyone can explain why there is this weird behavior with a private constructor and the uniform initialization:

struct Obj
{
 int a = 0;

private:
  Obj() = default;
};

Obj o; // Error (cannot access private member declared in class 'Obj')
Obj o{10}; // OK <-- Why???

I'm using VC19 with C++17

Thanks G.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Gian
  • 9
  • 2
    Up until the C++20 standard, your class `Obj` is an *aggregate* which means the second variant is doing [aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization) which bypasses the constructor. It has been "fixed" in the C++20 standard, in which the class is no longer an aggregate. – Some programmer dude Mar 20 '23 at 13:02
  • I tried to fix the title. c++ initialization is many things but not "simple". Title should summarize the question (and you need not mention c++) – 463035818_is_not_an_ai Mar 20 '23 at 13:04

0 Answers0