Container to pointers of inherited objects

Discussion in 'C++' started by axel22@gmail.com, Aug 13, 2006.

  1. Guest

    Hello.
    Please take a look at this piece of code:


    class baseC {
    //...
    };

    class derivedC : baseC {
    //...
    };


    int main() {
    baseC b1;
    derivedC d1;
    std::vector<baseC*> vex;

    vex.push_back(&b1);
    vex.push_back(&d1);


    return 1;
    }

    Compiler returns an error on the second push_back - type cast:
    conversion from 'derivedC * __w64' to 'baseC * const' exists, but is
    inaccessible.
    Can anyone tell me how to do this without the explicit cast, such as
    (baseC *)(&d1);

    thnx.
    , Aug 13, 2006
    #1
    1. Advertising

  2. David Harmon Guest

    On 13 Aug 2006 12:41:34 -0700 in comp.lang.c++,
    wrote,
    >Compiler returns an error on the second push_back - type cast:
    >conversion from 'derivedC * __w64' to 'baseC * const' exists, but is
    >inaccessible.


    class derivedC : public baseC {
    David Harmon, Aug 13, 2006
    #2
    1. Advertising

  3. Guest

    David Harmon je napisao/la:
    > On 13 Aug 2006 12:41:34 -0700 in comp.lang.c++,
    > wrote,
    > >Compiler returns an error on the second push_back - type cast:
    > >conversion from 'derivedC * __w64' to 'baseC * const' exists, but is
    > >inaccessible.

    >
    > class derivedC : public baseC {


    What do you know, it works. I thought public inheritance was the
    default inheritance.

    Thnx a lot!
    , Aug 13, 2006
    #3
  4. On 2006-08-13 16:08:24 -0400, said:

    >
    > David Harmon je napisao/la:
    >> On 13 Aug 2006 12:41:34 -0700 in comp.lang.c++,
    >> wrote,
    >>> Compiler returns an error on the second push_back - type cast:
    >>> conversion from 'derivedC * __w64' to 'baseC * const' exists, but is
    >>> inaccessible.

    >>
    >> class derivedC : public baseC {

    >
    > What do you know, it works. I thought public inheritance was the
    > default inheritance.


    It is default for struct, but not for class.

    --
    Clark S. Cox, III
    Clark S. Cox III, Aug 13, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xamalek
    Replies:
    7
    Views:
    677
  2. Marc Thrun
    Replies:
    15
    Views:
    842
    Tim Rentsch
    Oct 4, 2005
  3. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    642
  4. 7stud --
    Replies:
    11
    Views:
    372
    7stud --
    Nov 9, 2007
  5. Derek Basch
    Replies:
    4
    Views:
    177
    -berlin.de
    Aug 16, 2006
Loading...

Share This Page