473,472 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Inheritance, vectors and copy

I am slightly confused, so please keep that in mind if I make
only little sense.

Assume I have a base class:

class Base { ... };

and two derived classes:

class Int : public Base { ... int foo; };
class Double : public Base { ... double bar; };

Assume I want to keep a vector of Int's OR Double's.

I could write either
std::vector<Base *> v1;
or
std::vector<Base &> v2;

So far, so good?

I'm probably missing something fundamental: is there a real
difference between v1 and v2? Will v2 work with virtual functions?

I'll allocate several Int's and Double's on the heap with new
and store them in v2, for example.

Now I want to copy v2 into v3. If I just write v3 = v2, I'll end up with
the same references in v2 and v3, right? What I want is to copy every
object in the vector and store the reference to the copy in v3. That
way, if I modify an object in v3, v2 won't be affected.

One way I see is to overload operator=

typedef std::vector<Base &> MyVec;

MyVec &operator=(MyVec &)
{
for every element in MyVec, call a virtual self-copy method;
}

Is that acceptable? Is there a better way?

--
Regards, Nudge

Jul 22 '05 #1
3 1604
On Tue, 25 May 2004 23:10:47 +0200, Nudge <a@b.c> wrote:
I am slightly confused, so please keep that in mind if I make
only little sense.

Assume I have a base class:

class Base { ... };

and two derived classes:

class Int : public Base { ... int foo; };
class Double : public Base { ... double bar; };

Very clear.
Assume I want to keep a vector of Int's OR Double's.

I could write either
std::vector<Base *> v1;
Okay
or
std::vector<Base &> v2;
Not okay. References do not meet the requirements for elements of a
container; they're neither copyable nor assignable. Internally, STL
containers have to assign and copy their elements, and you just can't do
that with references. You'd never even be able to get them into a container
in the first place.

So far, so good?
Only the pointer version.

I'm probably missing something fundamental: is there a real
difference between v1 and v2? Will v2 work with virtual functions?
Let's forget about references in containers. For the rest of this
discussion, let's assume v2 is like v3: a container of Base *'s.

I'll allocate several Int's and Double's on the heap with new
and store them in v2, for example.
Okay.

Now I want to copy v2 into v3. If I just write v3 = v2, I'll end up with
the same references in v2 and v3, right? What I want is to copy every
object in the vector and store the reference to the copy in v3. That
way, if I modify an object in v3, v2 won't be affected.
If v3 is also a container of Base *'s, then yes, just saying
v3 = v2;
will copy the pointers and do nothing else. To replicate all the data, you
can just do it in a loop (untested code):

std::vector<Base *> v3;
for (int i = 0; i < v2.size(); ++i)
v3.push_back( v2[i]->clone());

assuming you've set up a cloning ("virtual copy constructor") mechanism for
objects in the Base hierarchy.


One way I see is to overload operator=

typedef std::vector<Base &> MyVec;

MyVec &operator=(MyVec &)
{
for every element in MyVec, call a virtual self-copy method;
}

Is that acceptable? Is there a better way?


See above.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Nudge wrote:
I am slightly confused, so please keep that in mind if I make
only little sense.

Assume I have a base class:

class Base { ... };

and two derived classes:

class Int : public Base { ... int foo; };
class Double : public Base { ... double bar; };

Assume I want to keep a vector of Int's OR Double's.

I could write either
std::vector<Base *> v1;
or
std::vector<Base &> v2;

So far, so good?
No, you cannot write

std::vector<Base &> v2;

References cannot be stored in a vector.
I'm probably missing something fundamental: is there a real
difference between v1 and v2? Will v2 work with virtual functions?
No, since 'v2' cannot exist, it will not work with virtual functions.
I'll allocate several Int's and Double's on the heap with new
and store them in v2, for example.
No, you cannot do that. Nobody can do that.
Now I want to copy v2 into v3. If I just write v3 = v2, I'll end up with
the same references in v2 and v3, right?
Nope, it cannot be done.
What I want is to copy every
object in the vector and store the reference to the copy in v3. That
way, if I modify an object in v3, v2 won't be affected.

One way I see is to overload operator=

typedef std::vector<Base &> MyVec;

MyVec &operator=(MyVec &)
{
for every element in MyVec, call a virtual self-copy method;
}

Is that acceptable? Is there a better way?


This is not acceptable. Your only option is to go with v1 (storing
the pointers to Base) and perform your own copying with simulated
"deep" copy of objects.

The problem is that (a) you cannot define the operator= that is not
a member of the class for which you try to define it and (b) the
existing operator= in 'std::vector' will not do the job correctly.

You need to roll out your own copy function.

Victor
Jul 22 '05 #3
[snip]

Thank you Leor and Victor. Your comments were VERY helpful.

Jul 22 '05 #4

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

Similar topics

6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
12
by: Dave Theese | last post by:
Hello all, I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks comparing vectors to plain old...
8
by: slurper | last post by:
if i have a vector and assign another vector to a vector variable like this: vector<int> c, k; c.push_back(1), c.push_back(2); k.push_back(3), k.push_back(4); c = k; does this work?? if...
9
by: pmatos | last post by:
Hi all, I have a few question regarding standard C++ and the use of vectors. Imagine: void foo(vector<int> x); vector<int> c; vector<int> x; ----------
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
5
by: roberts.noah | last post by:
It is my understanding that if you contain vectors or maps you don't need to create copy constructors because the default calls that constructor. It is my understanding that if you use these types...
4
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
2
by: joeme | last post by:
How would one using STL do the following tasks: 1) merge 2 sorted vectors with dupes, result shall be sorted 2) merge 2 sorted vectors without dupes, result shall be sorted 3) merge 2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 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.