Inheritence and Abstract classes in vectors

Hi,

I'm new to C++ and learning the ropes (coming from a Java background). I've tried to have a vector of abstract classes but the compiler chokes if i try and call the push_back method.

Heres what I'm doing (in simple terms), I have an abstract Account class (there are many different types of accounts but all have the same properties) which i need to store as a vector (as opposed to an array purely because our collection grows/shrinks) now I'm using several (pure) virtual functions in the Account.hpp file so it is an abstract class. Two implemented classes TaxAccount and SalesAccount are created and everything seems to work, unfortunately I cant add any instances of TaxAccount or Sales account to the vector of pointers.

Declared the abstract class and have this in my header:

std::vector accounts;

Adding it like so in accountHandler.cpp:

void AccountHandler::addAccount(const Account* item)
{
accounts.push_back(item);
}

VC2005 error:

Error 2 error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'const Account *' to 'Account *const &' x:UsersEmilyGDSBAAccountHandler.cpp 15

Any ideas?

Comments

  • use of const keyword is incorrect.
    [code]
    void AccountHandler::addAccount(Account* const item)
    [/code]
  • : use of const keyword is incorrect.
    : [code]
    : void AccountHandler::addAccount(Account* const item)
    : [/code]
    :

    Thanks for the reply:-)

    I've tried the fragment below and it seems to work:

    [code]
    void AccountHandler::addAccount(Account&item)
    {
    accounts.push_back(&item);
    }
    [/code]

    What's the difference between this fragement and yours if I may ask (not to be rude)?
  • : : use of const keyword is incorrect.
    : : [code]
    : : void AccountHandler::addAccount(Account* const item)
    : : [/code]
    : :
    :
    : Thanks for the reply:-)
    :
    : I've tried the fragment below and it seems to work:
    :
    : [code]
    : void AccountHandler::addAccount(Account&item)
    : {
    : accounts.push_back(&item);
    : }
    : [/code]
    :
    : What's the difference between this fragement and yours if I may ask (not to be rude)?
    :


    yours does not use const keyword at all, so the function is free to make any changes it wants to the object.
  • :
    : yours does not use const keyword at all, so the function is free to make any changes it wants to the object.
    :

    ah! that makes sense...

  • There are 4 kinds of pointers in C/C++:

    [code]
    Type* name; /* pointer to data */
    const Type* name; /* pointer to constant data */
    Type* const name; /* constant pointer to data */
    const Type* const name; /* constant pointer to constant data */
    [/code]

    A constant pointer is a pointer which you can't change the address of, ie you can't let it point at something else.

    In the case with vector::push_back, it wanted to be able to change the data, meaning that line 1 and 3 of the above pointer types would be acceptable, but not 2 and 4.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories