0

My parent class has a vector field

I want to force child classes to push in that vector as many as items they have. At least one item

example:

class Options {
    protected:
        vector<string> optionItems;
    public:
        void printItemsInConsole(){
            // prints items on console
        }
}

class MainOptions : public Options {
    //have to pushback {Open game, Open highScores, Exit} options for example

}

class SideOptions : public Options {
    // have to pushback {Clear game database, Uninstall game, Back to main menu} options for example
}

How to force child classes to push back their items?

I don't want to pass items in constructor and then put them in vector just right there, because some times items would be a lot, so it's not a clean way

6

You have no way to force a subclass or user of a class to perform certain actions after the constructor has completed. So any initialization work MUST be done in the constructor. There is rarely a good reason to ever have a half-initialized object.

Here, the typical way would be to have the base class take a collection as constructor parameter, e.g.:

#include <iostream>
#include <string>
#include <vector>

class Options {
protected:
    std::vector<std::string> items;
public:
    Options(std::vector<std::string>&& items) : items{std::move(items)} {}

    void printItemsInConsole(){
        for (auto& item : items)
          std::cout << item << '\n';
    }
};

class MainOptions : public Options {
public:
    MainOptions() : Options({"Open game", "Open Highscores", "Exit"}) {}

};

class SideOptions : public Options {
public:
     SideOptions() : Options({"Clear game data", "Uninstall", "Back to main menu"}) {}
};

int main() {
  MainOptions o;
  o.printItemsInConsole();
}

Note that the use of C++11 list initialization is a terse way for the subclass to provide these items. If you do not want to provide these items inline, you can always use a helper function to construct the vector.

1

Inheritance is not the most effective way to ensure that the base class object gets the data it needs. If you want to ensure that Options built for a purpose has certain data, then you should have its constructor be given that data. And then you provide certain functions that create different types of options:

class Options {
    private:
        vector<string> optionItems;

    public:

        Options(vector<string> &&opts) : optionItems(std::move(opts))
        {
            assert(!optionItems.empty());
        }

        void printItemsInConsole(){
            // prints items on console
        }
}

Options MainOptions()
{
    vector<string> opts;
    //have to pushback {Open game, Open highScores, Exit} options for example
    return Options(std::move(opts));
}

Options SideOptions()
{
    vector<string> opts;
    // have to pushback {Clear game database, Uninstall game, Back to main menu} options for example
    return Options(std::move(opts));
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.