casting pointers and dynamic binding

Discussion in 'C++' started by maynard, Sep 22, 2005.

  1. maynard

    maynard Guest

    How legit/acceptable is this bit of code?

    class Base{
    Base(){...}
    Base(const Base& b){...}
    };

    class D1: public Base
    {
    ....
    };

    class D2: public Base
    {
    ....
    };

    typedef Base* Bp;

    Bp* base = new Bp[100];
    base[0] = (D2*) new Base;
    base[2] = (D1*) new Base(*base[0]);
    maynard, Sep 22, 2005
    #1
    1. Advertising

  2. maynard wrote:
    > How legit/acceptable is this bit of code?
    >
    > class Base{
    > Base(){...}
    > Base(const Base& b){...}
    > };
    >
    > class D1: public Base
    > {
    > ...
    > };
    >
    > class D2: public Base
    > {
    > ...
    > };
    >
    > typedef Base* Bp;
    >
    > Bp* base = new Bp[100];


    OK, so 'base' is an array of 100 pointers to Base.

    > base[0] = (D2*) new Base;
    > base[2] = (D1*) new Base(*base[0]);


    I don't understand what this code is to do. Did you mean to write

    base[0] = new D2;
    base[1] = new D1;

    ? If not, what were you trying to accomplish?

    V
    Victor Bazarov, Sep 22, 2005
    #2
    1. Advertising

  3. maynard

    maynard Guest

    I didn't type it wrong...I need to construct a new object based on what
    kind of object is stored in the previous element (of base[]).
    Unfortunately, if I pass *base[0] to a copy constructor, the only one
    that will accept a reference to a Base object is the Base constructor
    (since base[] is an array of pointers to Base). For instance, I can't
    do this (which is what I really want):

    base[2] = new D2(*base[0]);

    So I *think* I'm forced to create a Base object and cast its pointer to
    the derived class I need. I've simplified this...in actuality, they're
    not all of type D2, some are of D1 and some are of D3, but I only need
    to make a copy when they're of type D2. Sorry, I know it's confusing.
    maynard, Sep 22, 2005
    #3
  4. maynard

    Rolf Magnus Guest

    maynard wrote:

    > I didn't type it wrong...


    But it doesn't make sense.

    > I need to construct a new object based on what kind of object is stored in
    > the previous element (of base[]).
    > Unfortunately, if I pass *base[0] to a copy constructor, the only one
    > that will accept a reference to a Base object is the Base constructor
    > (since base[] is an array of pointers to Base). For instance, I can't
    > do this (which is what I really want):
    >
    > base[2] = new D2(*base[0]);
    >
    > So I *think* I'm forced to create a Base object and cast its pointer to
    > the derived class I need.


    That doesn't magically convert the object into that derived class.

    > I've simplified this...in actuality, they're not all of type D2, some are
    > of D1 and some are of D3, but I only need to make a copy when they're of
    > type D2. Sorry, I know it's confusing.


    And what do you want to do if it's not of type D2?

    Anyway, the usual solution to copying polymorphic classes is a pure virtual
    member function in Base, and its implementations in the derived classes
    handle copying, like:

    class Base
    {
    public:
    virtual Base* clone() const = 0;
    };

    class D1: public Base
    {
    public:
    virtual D1* clone() const
    {
    return new D1(*this);
    }
    };

    class D2: public Base
    {
    public:
    virtual D2* clone() const
    {
    return new D2(*this);
    }
    };

    //...
    Base* base[100];
    base[0] = new D2;
    base[2] = base[0]->clone();

    Of course you can also make Base::clone a non-pure virtual function and
    implement it similar to the ones in D1 and D2, if Base is not supposed to
    be an abstract class.
    Rolf Magnus, Sep 22, 2005
    #4
  5. maynard wrote:
    > I didn't type it wrong...I need to construct a new object based on what
    > kind of object is stored in the previous element (of base[]).
    > Unfortunately, if I pass *base[0] to a copy constructor, the only one
    > that will accept a reference to a Base object is the Base constructor
    > (since base[] is an array of pointers to Base). For instance, I can't
    > do this (which is what I really want):
    >
    > base[2] = new D2(*base[0]);


    Why *can't* you? Are you not the programmer of that code?

    > So I *think* I'm forced to create a Base object and cast its pointer to
    > the derived class I need. I've simplified this...in actuality, they're
    > not all of type D2, some are of D1 and some are of D3, but I only need
    > to make a copy when they're of type D2. Sorry, I know it's confusing.


    Confusing? To whom?

    What you need is to define a constructor in D2 that accepts a Base and
    passes it along to the Base subobject's constructor. Same in D1 probably.
    And in all other classes.

    V
    Victor Bazarov, Sep 22, 2005
    #5
  6. maynard

    maynard Guest

    Thanks Victor...I feel like a moron...that is exactly what I need to
    do. It must be time to go home. Next time I'll think a little longer
    before I decide to post.
    maynard, Sep 22, 2005
    #6
  7. maynard

    maynard Guest

    > Why *can't* you? Are you not the programmer of that code?

    Actually, I'm not the programmer of Base, and I was trying to avoid
    modifying Base (however, if it was properly coded, it would've had a
    copy constructor...it doesn't).

    > Confusing? To whom?


    Obviously me.

    All in all, my only way around this is to implement one of your
    suggestions. I'll have to modify Base to some extent. Thanks to both
    of you.
    maynard, Sep 22, 2005
    #7
  8. maynard wrote:
    >>Why *can't* you? Are you not the programmer of that code?

    >
    >
    > Actually, I'm not the programmer of Base, and I was trying to avoid
    > modifying Base (however, if it was properly coded, it would've had a
    > copy constructor...it doesn't).


    But whether Base has a user-defined copy-c-tor or not is _irrelevant_.
    You need to add constructors *from* Base to D1, D2, etc.:

    class D1 : public Base {
    ...
    D1(const Base& b) : Base(b), ...
    };

    class D2 : public Base {
    ...
    D2(const Base& b) : Base(b), ...
    };

    which will allow you to do

    base[1] = new D1(base[0]);

    > [...]


    V
    Victor Bazarov, Sep 22, 2005
    #8
  9. maynard

    Default User Guest

    maynard wrote:

    > I didn't type it wrong...


    Type what wrong?



    Brian
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Sep 22, 2005
    #9
  10. maynard

    Axter Guest

    maynard wrote:
    > How legit/acceptable is this bit of code?
    >
    > class Base{
    > Base(){...}
    > Base(const Base& b){...}
    > };
    >
    > class D1: public Base
    > {
    > ...
    > };
    >
    > class D2: public Base
    > {
    > ...
    > };
    >
    > typedef Base* Bp;
    >
    > Bp* base = new Bp[100];
    > base[0] = (D2*) new Base;
    > base[2] = (D1*) new Base(*base[0]);


    You can do this if you use clone type smart pointers instead of raw
    pointers, and if you use vector instead of a raw pointer to an array of
    pointers.
    The following links have some clone type smart pointers.
    http://code.axter.com/copy_ptr.h
    http://code.axter.com/cow_ptr.h

    Example code:
    std::vector<copy_ptr<Base> > vBase;
    vBase.push_back(new D1);
    vBase.push_back(new D2);
    vBase.push_back(vBase[0]);
    vBase.push_back(vBase[1]);
    vBase[0] = vBase[1]; //vBase[0] will now be of D2 type
    vBase[1] = vBase[2]; //vBase[1] will now be of D1 type

    If you use the cow_ptr, it's more efficient in that it uses reference
    counting, until you try to access the -> operator.
    Axter, Sep 23, 2005
    #10
    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. kevin
    Replies:
    11
    Views:
    5,786
    Andrew McDonagh
    Jan 8, 2005
  2. Alexander Schmolck
    Replies:
    2
    Views:
    361
    David Rushby
    Jan 28, 2006
  3. Wally Barnes
    Replies:
    3
    Views:
    511
    Wally Barnes
    Nov 20, 2008
  4. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    551
    James Kanze
    Dec 20, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    657
Loading...

Share This Page