Sign in to follow this  
lubby

pure virtual templated methods?

Recommended Posts

Hi there, thanks for taking time to read my post :) I have an abstract base class lets call it Base, Base has a pure virtual method called DoSomething, DoSomething has a parameter T, its type is unknown, so I would like to implement this with templates, rather than overriding DoSomething for each possible type. Only problem is I do not know the syntax for this (C++). Base must remain abstract, and the DoSomething method must remain pure virtual.
class Base
{
  Base();
  virtual ~Base();

  virtual void DoSomething(T& value) = 0;
};

class Child
{
  Child();
  virtual Child();

  void DoSomething(T& value) { value *= value ; }
};

Share this post


Link to post
Share on other sites
[code lang="c++"]
template <class T>
class Base
{
Base();
virtual ~Base();

virtual void DoSomething(T& value) = 0;
};
[/code]

?

I think thats what you want, but I don't do C++. Linky

Hth,

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Unfortunately virtual member template functions are not allowed in C++ because of the difficulties it would cause in implementing efficient compiling and linking.

You can make a virtual function take a type specified in the class template as the poster above shows, but making the virtual function itself a template function is not allowed.

Share this post


Link to post
Share on other sites
I only wish it were that simple :( My fault, i did not fully explain my problem assuming it would be isolated to this single class.

By adding the template <class T> i can indeed get futher, however, errors still abound. At this stage i do not know the type, as it will not be used until b->DoSomething() is called in the instance of SomethingElseChild.



class SomethingElseBase
{
// compile error here
// error C2955: 'Base' : use of class template requires template argument list
virtual bool DoSomethingElseData(Base *b) = 0;
};

class SomethingElseChild : public SomethingElseBase
{
bool DoSomethingElse(Base *b)
{
int a = 0;
float b = 10.0f;
long c = 100.0;

b->DoSomething(a);
b->DoSomething(b);
b->DoSomething(c);
}
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Unfortunately virtual member template functions are not allowed in C++ because of the difficulties it would cause in implementing efficient compiling and linking.

You can make a virtual function take a type specified in the class template as the poster above shows, but making the virtual function itself a template function is not allowed.


Thanks, that then answers my question. Looks like I am going to have to overload DoSomething or make it a c style global function.

Share this post


Link to post
Share on other sites
Another solution could be to make the value you want to manipulate a member variable and pass it in the constructor.


class Base {
public:
virtual void DoSomething();
};

template <class T>
class Derived : public Base {
private:
T MyValue;
public:
Derived(T init) {
MyValue = init;
}
void DoSomething() {
// Do something with MyValue here
}
};

Derived<int> foo(100);
foo.DoSomething();


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sometimes it's just not tha language that's wrong, it's your design. Maybe you should sit down an think about refactoring your code.

No offense, just an advice.

Share this post


Link to post
Share on other sites
Quote:

Another solution could be to make the value you want to manipulate a member variable and pass it in the constructor.


Thanks for the hint DaTroof, unfortunatelly this would not suite my needs as the type MyValue would still need to be known to construct Derived. The problem I have is that the type will be unknown until after construction and the class is in use. Also, the type passed into the method will change. So a single instance of Derived:: where DoSomething handles any type is needed.

Quote:

Sometimes it's just not tha language that's wrong, it's your design. Maybe you should sit down an think about refactoring your code.

No offense, just an advice.


None taken, it is good to take a step back and re-evaluate the design, and in a situation like this, there is always the thought - 'is there a better way to do this?'. What I am attempting here is definately outside the box, and would be most probably be considered a hack by most. If you could see all the macros this stuff was wrapped up in you would slap me with the hack hat. :) The functionality this code will provide, is however quite powerfull, and is worth this amount of unintentional obfuscation.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this