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