473,499 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

casting pointers and dynamic binding

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

Sep 22 '05 #1
9 1486
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
Sep 22 '05 #2
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.

Sep 22 '05 #3
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.

Sep 22 '05 #4
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
Sep 22 '05 #5
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.

Sep 22 '05 #6
> 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.

Sep 22 '05 #7
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
Sep 22 '05 #8
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.
Sep 22 '05 #9
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.

Sep 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1543
by: Andy Franks | last post by:
Hi All, This is driving me nuts, especially since I have this working for my main application with no problem. I suspect it might be due to Namespace conflicts, but I'm not positive. Any help...
2
3394
by: festiv | last post by:
Hi there, I want to learn how the compiler is implementing the dynamic binding. where can i read about this subject (the hole process). thanks.
7
3017
by: mead | last post by:
The code is from a Meyers' book... class BankAccount { ... }; // as above // new class representing accounts that bear interest class InterestBearingAccount: public BankAccount { public:...
14
1630
by: Shannon Richards | last post by:
Hello: I am confused about casting... Option Strict = ON Example1: --------------- When I say something like this... Dim lo_Foo = New Foo Dim lo_Obj as Object = lo_Foo
6
1561
by: Brett | last post by:
I find there is more casting required in C# than VB.NET. If Option Strict/Explicit is turned on, will this basically create the same environment as C# - uppercase, more casting required, must...
11
4670
by: redefined.horizons | last post by:
First, I would thank all of those that took the time to answer my question about creating an array based on a numeric value stored in a variable. I realize after reading the responses and doing...
13
3019
by: DaTurk | last post by:
Hi, This is a question brought about by a solution I came up with to another question I had, which was "Dynamic object creation". So, I'm curious if you can dynamically cast an object. If you...
2
2667
by: keith | last post by:
Hello, Could someone please explain why the (GCC, ancient 2.95.3 version) compiler won't let me static_cast<a char* to a unsigned char* or vice versa? It also won't accept dynamic_cast<for...
9
3451
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.