Newbie using vectors

E

ernesto

Hi everybody:

I'm newbie using the STL std:vector container.

Let's say I have:

class A
{
public:
A();
A(const A& a);
virtual ~A();
};


class B : public A
{
public:
B();
B(const B& b);
virtual ~B();
private:
int val;
};


My questions are:

1. Can I define something like:
std::vector<A> myVector;
A a;
B b;
myVector.push_back(a);
myVector.push_back(b);
?

2. If I can define it, how is the data internally stored inside the
vector? using an A** array or using an A* array? If is using an A*
array, how can it allocate memory space for objects of, probably,
different size (sizeof(A) != sizeof(B)?

3. If I have a hierarchy of classes, every class containing a lot of
attributes (some primitive attributes and some contained objects), what
is the better way of implementing the copy constructors?
- Using some Smart Pointer with reference counting?
- Copy all the attributes (no matter what heavy this process can
be?)
- Using some simple smart pointer (ptr<T>) ?
- Another approach?

Best regards,



Ernesto
 
K

Kai-Uwe Bux

ernesto said:
Hi everybody:

I'm newbie using the STL std:vector container.

Let's say I have:

class A
{
public:
A();
A(const A& a);
virtual ~A();
};


class B : public A
{
public:
B();
B(const B& b);
virtual ~B();
private:
int val;
};


My questions are:

1. Can I define something like:
std::vector<A> myVector;
A a;
B b;
myVector.push_back(a);
myVector.push_back(b);
?

No. Standard containers have value semantics and are no polymorphic. If you
want to use containers for a class hierarchy, you need to do something
like:

(a) std::container<T*>. This gives you polymorphism. You will run into a
few tricky issues: (1) resource management is up to you and (2) whenever a
standard algorithm uses a functor or predicate, you have to manually take
care of forwarding the action from the pointer to the pointee (e.g., sort
will sort the vector by value of the pointer not by value of the pointee).

(b) std::container< some_smart_pointer<T> >. Depending on the smart pointer
this may ease some of the headaches in (a). The choice of the smart pointer
is somewhat tricky but mostly determined by the semantic constraints for
your container.

(c) some_pointer_container<T>. There are several implementations out there.
I think, Boost has one. You should check out this alternative.

2. If I can define it, how is the data internally stored inside the
vector? using an A** array or using an A* array? If is using an A*
array, how can it allocate memory space for objects of, probably,
different size (sizeof(A) != sizeof(B)?
Moot.

3. If I have a hierarchy of classes, every class containing a lot of
attributes (some primitive attributes and some contained objects), what
is the better way of implementing the copy constructors?
- Using some Smart Pointer with reference counting?
- Copy all the attributes (no matter what heavy this process can
be?)
- Using some simple smart pointer (ptr<T>) ?
- Another approach?

That cannot be answered in general. How to implement copy constructor,
assignment operator and operator== very much depends on the "criterion for
identity" that you want for objects of your class. This is not an
implementation detail but a central part of the semantics of your class.
You need to spell out what objects are the same and which are different
clearly in advance. Then, you need to implement all three functions
accordingly. Only after the semantics is pinned down, one can talk about
more or less advantageous implementations.


Best

Kai-Uwe Bux
 
D

David Harmon

On 8 Oct 2006 17:20:53 -0700 in comp.lang.c++, "ernesto"
1. Can I define something like:
std::vector<A> myVector;
A a;
B b;
myVector.push_back(a);
myVector.push_back(b);

The last line causes the value of b to be converted down to a type A
object and stored. Vectors, like arrays, can literally contain only
one type of object. Polymorphic contents requires a vector of
pointers to the base type, or some other solution.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[34.4] How can I build a <favorite container> of objects of
different types?" It is always good to check the FAQ before
posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

Vector of raw pointers puts the memory management headaches on you.
boost::shared_ptr or tr1::shared_ptr can handle that for you.
http://boost.org
3. If I have a hierarchy of classes, every class containing a lot of
attributes (some primitive attributes and some contained objects), what
is the better way of implementing the copy constructors?
- Using some Smart Pointer with reference counting?
- Copy all the attributes (no matter what heavy this process can
be?)
- Using some simple smart pointer (ptr<T>) ?
- Another approach?

This really depends on your class's behavior; i.e., what does it
mean to copy one of them to another in your application.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top