1

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?

1 Answer 1

3

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" })
{
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to put a full list? [code]B::B() : a(std::vector<std::string>("var1","var2")) doesn't work[\code]
Thank you! I've been trying B::B() : a(std::vector<std::string>{ "File", "Edit", "Options", "Help" })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.