Redis1001
Redis1001

Reputation: 157

Pushing derived class to vector of base class in c++

I have the following code:

class Official {
    public:
        explicit Official(int iRow, int iColumn, int iRankHolder, int** aiBoardHolder);
};

class General : public Official {
    public: 
        explicit General(int iRow, int iColumn, int iRankHolder, int** aiBoardHolder) : Official(iRow, iColumn, iRankHolder, aiBoardHolder) {};
};

class Map {
    private:
        std::vector<Official> aoOfficialStack;

    public:
        void generateOfficialObject();
};

void Map::generateOfficialObject() {
    aoOfficialStack.push_back(General(1, 2, 3, aiBoardPosition));
}

Question is why am I getting this error after calling generateOfficalObject()?

Error C2664 'void std::vector>::push_back(const Official &)': cannot convert argument 1 from 'General' to 'Official &&' Project c:\users\user\desktop\project\project\board\board.cpp 12

Thank you very much!

Upvotes: 1

Views: 1896

Answers (2)

Hayt
Hayt

Reputation: 5370

It's a c++ restriction. You cannot assign the value of a derived class to it's base class without consequences. This may compiles but will result in some unexpected behaviors. It could copy the object into an instance of the base class (this losing the information it has from the derived class). This is called object slicing

You have to use references or pointers for this. So you can change your vector to

std::vector<std::unique_ptr<Official>> aoOfficialStack;

and assign like:

aoOfficialStack.push_back(std::make_unique<General>(1, 2, 3, aiBoardPosition));

a shared_ptr would also be an alternative it depends on the usage.

Upvotes: 0

eerorika
eerorika

Reputation: 238461

Your example program compiles. Either your example is incomplete, or your compiler isn't standard compliant.

The program is well formed and has defined behaviour. The behaviour might not be what you expect however. Object slicing is an obscure language feature that beginners may find counterintuitive. It makes no sense to create an instance of General, if you're only going to use the Official sub object. Ask yourself: why not create an instance of Official in the first place?

Upvotes: 1

Related Questions