Home > OS >  return single object from unique_ptr vector in c
return single object from unique_ptr vector in c

Time:12-17

I'm making a command applaction I have base class that other commands class inherate.

struct BaseCommand {
public:
    virtual ~BaseCommand() {}
    int id = 0;
    std::string name = "Base Command";
    std::string description = "Base Command";
    BaseCommand();
    virtual std::string getName();
    virtual int getId();
    virtual std::string getDescription();
private:
    virtual void init();
    virtual void run();
    
};
class HelpCommand: public BaseCommand {
public:
    HelpCommand();
    std::string name = "help";
    std::string description = "output help";
    int id = 1;
    virtual std::string getName();
    int getId() {return this->id;}; 
    virtual std::string getDescription();
    
private:
    virtual void init();
    virtual void run();
};
class ProductCommand: public BaseCommand {
public:
    ProductCommand();
    std::string name = "prod";
    std::string description = "product";
    int id = 2;
    virtual std::string getName();
    int getId() {return this->id;};
    virtual std::string getDescription();
    
private:
    virtual void init();
    virtual void run();
};

and in my main, I push my subclass to a vector. my goal is to get the command by its name how do I do that and get the only command and assign it to a variable

    std::vector<std::unique_ptr<BaseCommand>> commands;
    // lett's push our commands...
    commands.emplace_back(new HelpCommand);
    commands.emplace_back(new ProductCommand);
    std::string command = 'prod';

    // what's the type should be for selectedCommand?
    ?? selectedCommand;

    for (int i = 0; i < commands.size();   i) {
        if (commands[i]->getName() == command) {
             selectedCommand = commands[i];
        }
   }

I can't seem to determine which type should the selectedCommand be. please what I'm missing here?

I can't seem to determine which type should the selectedCommand be. please what I'm missing here?

CodePudding user response:

You can't "override" member variables.

Your derived classes have two of each member variable with the same name; one in the base class and one in the derived class.
When you access the objects through a pointer to BaseCommand, you get the members from BaseCommand and not those from the dervied class.

Redesign so these are only members of the base, and make the accessors non-virtual:

struct BaseCommand {
public:
    virtual ~BaseCommand() {}
    const std::string& getName() const { return name; }
    int getId() const { return id; }
    const std::string& getDescription() const { return description; }

protected:
    // Let only derived classes create base instances.
    BaseCommand(int id,
                const std::string& name,
                const std::string& description)
        : id(id), name(name), description(description)
    {}

private:
    int id = 0;
    std::string name = "Base Command";
    std::string description = "Base Command";

    virtual void init();
    virtual void run();
};

class HelpCommand: public BaseCommand {
public:
    HelpCommand() : BaseCommand(1, "help", "output help") {}
private:
    virtual void init();
    virtual void run();
};
class ProductCommand: public BaseCommand {
public:
    ProductCommand() : BaseCommand(2, "prod", "product") {}
private:
    virtual void init();
    virtual void run();
};
  • Related