Lucas
Lucas

Reputation: 367

how to init 2 memebers in nested classes in C++

Hi my win32 question is as follows: Having 2 classes: class A, and a nested Class B. Each class has a HWND member. I want to init both A and B HWND member in constructor BEFORE is called. I mean:

class A {
    HWND hwnd_main;
    B b;
public:
    A(HWND hwnd) : hwnd_main(hwnd), B(hwnd) {}
};

class B {
    HWND hwnd_main;
public:
    B(HWND hwnd)
    {
        hwnd_main = hwnd;
    }
};

The thing that with this scheme constructor of Class "B" is called. Isn't a way to fill the constructor of "B" without calling it specifically? Thx.

Upvotes: 0

Views: 51

Answers (1)

P.W
P.W

Reputation: 26800

What you have here is not a nested class but more like an aggregate class. There is also no inheritance involved here (class B is not a direct base of A), so you cannot call constructor of class B directly in the constructor of class A and initialize its variables.

Instead, you have to initialize the variable b which is a member of class A like this:

A(HWND hwnd) : hwnd_main(hwnd), b(hwnd) {}

Upvotes: 1

Related Questions