Can it be done in a nice way?
template <class T> Init(T &object, Type type)
{
Collider collider;
switch (type)
{
case TypeA:
collider.InitAsA(object);
break;
case TypeB:
collider.InitAsB(object);
}
}
call with
A a;
Init(a, TypeA);
B b;
Init(b, TypeB);
A and B have no common ancestor.
void Collider::InitAsA(A &a);
void Collider::InitAsB(B &b);
EDIT:
Collider::InitAs.. can't be changed into template, and can't be changed at all, for reason i can't change others code.
Type can be int, actually it is an enum.
With your Init()
function, you can just use function overloading:
void Init(A &object)
{
Collider collider;
collider.InitAsA(object);
}
void Init(B &object)
{
Collider collider;
collider.InitAsB(object);
}
However, you could just overload the Init()
method:
void Collider::Init(A &a);
void Collider::Init(B &b);
And then, if you still want a template function, it could look like this:
template <typename T>
void Init (T &object)
{
Collider collider;
collider.Init(object);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.