0

I've got a question concerning handling of virtual function in C++ programming. I have something like this:

template<class T>
class baseClass
{
    virtual void doSomething(T& t) {
        // some baseClass specific code
    }

    void doSomethingElse(T& t) {
        // some baseClass specific code

        this->doSomething(t);
    }
}

template<class T>
class subClass
{
    virtual void doSomething(T&) {
        // some subclass related code
    }
}

Now, if I construct an object of type subClass....

int main(int argc, char *argv[])
{
    subClass<anyType> * subClassObject = new subClass<anyType>();
    subClassObject->doSomethingElse(anyTypeObject);
}

.... and call the doSomethingElse method of the base class, this method will call the doSomething method of the base class and not of the sub class.

What I want to have, is calling the doSomething method of the subclass (not of the baseClass).

Can anybody tell me how to accomplish that?

Servy
  • 202,030
  • 26
  • 332
  • 449
Pythonic
  • 27
  • 1
  • 3
  • 1
    How does this code compile? `subClass` isn't related to `baseClass` and doesn't have a `doSomethingElse` method. – Mark B Jul 31 '14 at 15:30

2 Answers2

3

You could accomplish it with CRTP:

template<class T, class Derived>
class baseClass
{    
    void doSomethingElse(T& t) {
        // some baseClass specific code
        static_cast<Derived*>(this)->doSomething(t);
    }
}

template<class T>
class subClass : public baseClass<T, subClass>
{
    void doSomething(T&) {
        // some subclass related code
    }
}

See this for discussion about virtual template methods

Community
  • 1
  • 1
Roman Saveljev
  • 2,544
  • 1
  • 21
  • 20
  • Thanks for your support! If I do not have to make these functions virtual, and just override doSomething by the subClass, will doSomethingElse call doSomething of baseClass or subClass then? – Pythonic Aug 07 '12 at 15:30
  • Please pay attention. In my variant you do no override doSomething in subClass. baseClass does not have doSomething anymore. baseClass::doSomethingElse could well call any member via ordinary non-virtual call – Roman Saveljev Aug 07 '12 at 15:34
  • Ok I got you! Thanks for your support. Seems like a good alternative. – Pythonic Aug 07 '12 at 15:37
0

I made several changes to your code to get it to compile, and with g++ 4.5 it indicates the subclass method is being called. Here's the code I compiled:

#include <iostream>

template<class T>
class baseClass
{
public:
    virtual void doSomething(T& t) {
        std::cout << "Base" << std::endl;
        // some baseClass specific code
    }

    void doSomethingElse(T& t) {
        // some baseClass specific code

        this->doSomething(t);
    }
};

template<class T>
class subClass : public baseClass<T>
{
    virtual void doSomething(T&) {
        std::cout << "Child" << std::endl;
        // some subclass related code
    }
};

int main()
{
    typedef int anyType;

    subClass<anyType> * subClassObject = new subClass<anyType>();
    int foo;
    subClassObject->doSomethingElse(foo);
}
Mark B
  • 95,107
  • 10
  • 109
  • 188