12

I have a base class and a derived one and I want to change base functions while keeping them static as they should be passed to other functions as static.

How can I do that?

Antonio Pérez
  • 6,702
  • 4
  • 36
  • 61
fank
  • 131
  • 2
  • 4
  • 1
    Give a concrete, minimal and complete example of what you are trying to achieve (_not_ what you think you want to implement), and we can think about how to do that. – Kerrek SB Jun 09 '11 at 10:23

7 Answers7

26

The ATL framework gets around the limitation of no virtual statics by making the base class be a template, and then having derived classes pass their class type as a template parameter. The base class can then call derived class statics when needed, eg:

template< class DerivedType >
class Base
{
public:
  static void DoSomething() { DerivedType::DoSomethingElse(); }
};

class Derived1 : public Base<Derived1>
{
public:
  static void DoSomethingElse() { ... }
};

class Derived2 : public Base<Derived2>
{
public:
  static void DoSomethingElse() { ... }
};

This is known as Curiously recurring template pattern, which can be used to implement static polymorphism.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • This design is exactly what I need, but there's a problem. How can you use `Base` when you're declaring `class Derived1` ? I mean, this cause me errors : `error: expected a type, got ‘Derived1’` and `error: type/value mismatch at argument...`. – Gabriel L. Feb 02 '14 at 15:41
  • Try changing `typename` to `class`. And read this: [Curiously recurring template pattern](http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern). – Remy Lebeau Feb 02 '14 at 16:43
  • Nice one ! I didn't know about that design. That's working now, thank you very much. I already +1 your answer. – Gabriel L. Feb 02 '14 at 17:16
  • Great answer! However, with this approach it is not possible anymore to do something like: `Base *b; if(cond) b = new Derived1(...); else b = new Derived2(...)`. Is there a workaround to still be able to use polymorphism in that way? Thank you! – Rastapopoulos Jun 14 '18 at 14:57
  • 1
    @Rastapopoulos create a non-template base class for `Base` to derive from – Remy Lebeau Jun 14 '18 at 15:51
3

Do you mean you need a pointer to a static function (e.g. to pass as an argument to another function that requires a pointer to a static function), but you need to access that function pointer virtually? In that case, use a virtual function to get the function pointer:

typedef void (*function)();
void do_stuff_with_function(function);

struct Base {
    virtual ~Base() {}
    virtual function get_function() = 0;
};

struct Derived : Base {
    function get_function() {return my_function;}
    static void my_function();
};

Derived d;
do_stuff_with_function(d.get_function());
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

static function can not be virtual since they do not have an instance through which they are accessed. I do believe you can overwrite them though.

Mario The Spoon
  • 4,799
  • 1
  • 24
  • 36
  • i know that i can not use static and virtual at the same time but i have to do something like that – fank Jun 09 '11 at 10:19
  • there is just no way to do it. either use the function pointer trick (which I would strongly discourage) or remove the static constraint. If the static method is used to create an instace, have alook at the factory pattern (http://programmerjoe.com/2007/03/18/the-abstract-factory-pattern-in-c/) – Mario The Spoon Jun 09 '11 at 10:22
1

You can't have static virtual functions in C++.

Asha
  • 11,002
  • 6
  • 44
  • 66
1

Virtual functions typically rely on this pointer to determine the type of function to be called at run time.

A static member function does not pass a this so static virtual functions are not allowed in C++.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

If i am correct in understanding ur question, then u can follow the following approach otherwise ignore..

have static function pointer in the base class.

in base class have a static function ( in which u call the function by using that static function pointer)..

in derived classes set that static function poiter to the function defination u wish to execute.. ( in base class u can set the function pointer to some default function).

Nik
  • 695
  • 1
  • 4
  • 15
  • i did something like that i make a static function and a virtual one in the base class, and the static calls the virtual one after it changed in the derived class but now the problem is the virtual does not go to derived class so it does not change – fank Jun 09 '11 at 10:23
  • @frank hi, i am not asking u to have a virtual function ... i want u to have a static function pointer, and a setter for that variable... which u call from ur subclasses.... and in ur main static function u just call the function using that function pointer. – Nik Jun 09 '11 at 10:28
-1

You cannot have static virtual functions, because it doesn't make sense to have them.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 7
    Actually, it can make sense: it's like any other virtual function where the implementation changes based on the derived type of the class. It's just that you don't have an instance. [Delphi](http://docwiki.embarcadero.com/RADStudio/en/Methods#Ordinary_Class_Methods), for example, allows it and they can be rather useful. – David Jun 10 '11 at 02:23