Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C++] Overriding template class function

Started by Florian22222 Dec 22, 2011 at 9:15 AM 1 replies 450+ views
Original Post
Florian22222
Florian22222
Hello!
I have a template class:

template<class TYPE>
class Dynamic
{
public:
Dynamic(){ Create(); };
virtual void Create() = 0;
};

class Triangle : public Dynamic<int>
{
public:
virtual void Create()
{
//do something
};
};



if i compile this, i get an linker error for create().
if i use

virtual void Create(){}; //in dynamic


instead, the create from dynamic is called and not from triangle.

Please help me, i cant cope with that.
wqking
wqking
Don't use virtual function in constructor.
If you do use, the function is not polymorphism, it's one defined in current class.
If you really need to call virtual function, use named constructor idiom.


class MyClass {
public:
static MyClass * newMyClass() {
MyClass * instance = new MyClass;
instance->someVirtualFunction();
return instance;
}
};
Florian22222
Florian22222
thank you!
I now made an extra method Create()

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.