casting pointers and dynamic binding

M

maynard

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]);
 
V

Victor Bazarov

maynard said:
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
 
M

maynard

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.
 
R

Rolf Magnus

maynard said:
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.
 
V

Victor Bazarov

maynard said:
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
 
M

maynard

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.
 
M

maynard

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.
 
V

Victor Bazarov

maynard said:
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
 
A

Axter

maynard said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top