user2933244
user2933244

Reputation:

How to construct a class within another class? (simple code)

I don't know what to put in the ??? spot. Here's the code:

class A
{
    public:
        A(std::vector <std::string> init);
}

class B
{
    public:
        B();
    private:
        A a;
}

B::B() : a(???)
{
}

If you want some background, class A is menu that takes a vector of button titles, and class B is the MenuState superclass that manages both menu and some additional stuff. Or it's just my design that is flawed?

Upvotes: 1

Views: 73

Answers (1)

Ralph Tandetzky
Ralph Tandetzky

Reputation: 23640

Just write std::vector<std::string>() where you wrote ???. This way you will have an empty list there. Otherwise, if you want to fill it right at construction, you may write a function call like generateButtonTitles() there and define that function in a proper place.

B::B() : a(generateButtonTitles())
{
}

If you use a C++11 compliant compiler, then you can also pass an initializer list in the following way:

B::B() : a({ "File", "Edit", "Options", "Help" })
{
}

Upvotes: 3

Related Questions