typecasting in std::vector type
-
Hi, I have using a source code representing a class hierarchical like the following:
class c1
{...};
class c2 : public c1
{...};now there is a method in another class which has argument like:
void Func1(vector<c1*>& vC1);
How could I pass the following vC2 to this method
vector<c2*> vC2;
Is there any way to typecast the vector<c2*> to vector<c1*>?
-
Hi, I have using a source code representing a class hierarchical like the following:
class c1
{...};
class c2 : public c1
{...};now there is a method in another class which has argument like:
void Func1(vector<c1*>& vC1);
How could I pass the following vC2 to this method
vector<c2*> vC2;
Is there any way to typecast the vector<c2*> to vector<c1*>?
You could try to declare your function as:
void Func1(vector<c1*>* pC1Vector);
and then pass a casted pointer :-O :cool: :) :
{
vectror<c2*> vC2;
...
Func1((vector<c1*>*) &vC2);
}virtual void BeHappy() = 0;
-
You could try to declare your function as:
void Func1(vector<c1*>* pC1Vector);
and then pass a casted pointer :-O :cool: :) :
{
vectror<c2*> vC2;
...
Func1((vector<c1*>*) &vC2);
}virtual void BeHappy() = 0;
-
Hi, I have using a source code representing a class hierarchical like the following:
class c1
{...};
class c2 : public c1
{...};now there is a method in another class which has argument like:
void Func1(vector<c1*>& vC1);
How could I pass the following vC2 to this method
vector<c2*> vC2;
Is there any way to typecast the vector<c2*> to vector<c1*>?
Short answer - no. Just because c2 is convertible to c1 doesn't mean that types dependent on c2 (like vector in this case) can be converted to equivalent types dependent on c1.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!
-
Maybe, it would be better (possible) to collect your
c2
-pointers to avector<c1*> vC1;
instead ? :)virtual void BeHappy() = 0;