Jump to content
  • Advertisement
Sign in to follow this  
Florian22222

[C++] Overriding template class function

This topic is 2554 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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;
}
};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!