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]); 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
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 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.
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
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.
> 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 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
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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.
|
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:...
|
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
|
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...
| |
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...
|
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...
|
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...
|
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
|
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,...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |