"f.f" <ah******@gmail.comwrote in news:1167836437.480073.131750
@i12g2000cwa.googlegroups.com:
class A
{
...
};
class B : public A
{
...
};
class C : public A
{
...
};
T* createObject()
{
T* ptr = new T();
return ptr;
}
I wanna use createObject() to create object instance of either class B
or class C?
What approach should I use? Should I use template?
How does createObject know which of A, B, or C to create? Offhand I
don't see how a template makes it easier:
template<class T>
T * createObject()
{
return new T();
}
All this seems to do is obfuscate the code by hiding the use of "new",
since you'd have to specify the type to be created at the call site
anyway. Why not just use "new" whererever you're using "createObject"?