How to initialize anonymous structures in class constructor?

  anonymous-types, c++, struct

How could i initialize anonymous structures ?

class A
{
public:
    A() : m_var.var_1(1)
    {
    }
    
private:
    struct 
    {
        int var_1;
        int var_2;
    }m_var;
};

The above code give me this error when i try to compile it:

main.cpp: In constructor ‘A::A()’:
main.cpp:6:16: error: expected ‘(’ before ‘.’ token
    6 |     A() : m_var.var_1(1)
      |                ^
      |                (
main.cpp:6:16: error: expected ‘{’ before ‘.’ token

Source: Windows Questions C++

LEAVE A COMMENT