N
Nafai
Hello. I'll try to explain my problem with an example:
I have the following classes:
class A {
public:
string name;
....
};
class B : public A
{
B(int x) {b=x; name="I am B"; }
int b;
getB() { return b };
};
class C : public A
{
C(int x) {c=x; name="I am C"; }
int c;
getC() { return c; }
};
Now, I need a vector<A*> v;
An there is a function like the following:
void p()
{
...
if(v->name=="I am B"){ v->getB() ...};
...
if(v[j]->name=="I am C") { v[j]->getC() ... };
...
};
It doesn't work because v contains objects of class A, but A doesn't
implement getB() nor getC().
But I need to have in the same vector objets of A, B and C. And I don't want
to declare getB and getC as virtual in A, because these methods will only be
defined in classes B and C respectively.
How can I do that?
Thanks.
I have the following classes:
class A {
public:
string name;
....
};
class B : public A
{
B(int x) {b=x; name="I am B"; }
int b;
getB() { return b };
};
class C : public A
{
C(int x) {c=x; name="I am C"; }
int c;
getC() { return c; }
};
Now, I need a vector<A*> v;
An there is a function like the following:
void p()
{
...
if(v->name=="I am B"){ v->getB() ...};
...
if(v[j]->name=="I am C") { v[j]->getC() ... };
...
};
It doesn't work because v contains objects of class A, but A doesn't
implement getB() nor getC().
But I need to have in the same vector objets of A, B and C. And I don't want
to declare getB and getC as virtual in A, because these methods will only be
defined in classes B and C respectively.
How can I do that?
Thanks.