I have read the following SO questions:
- Calling derived class function from base class
- Call derived class method from base class reference
- What is object slicing?
however I have a slightly different issue regarding static methods and the difference between Visual-c++ and gcc.
I have a class which bundles a few static methods related to a given task. I want to implement a simple version of each method, and allow a more efficient implementation in the future. I do this by implementing all the methods in a base class, and calling the methods from the derived class. This way methods which have 'newer' implementation will hide the 'old' implementation. My problem is that when one method g()
calls another method f()
, I want to verify that the implementation in the derived class is called (if it exists). I cannot use virtual methods, since my methods are all static. In visual-c++ the following code is doing what I have just described, but gcc won't compile - actually I am surprised that visual-c++ compiles OK. (I need my code to compile on both)
struct Base {
static void f () {}
static void g () { Derived::f(); } //--- visual-c++ compiles ok; gcc: "'Derived' was not declared in this scope"
};
struct Derived : public Base {
static void f () {}
};
Derived::g ();
My question is, how come visual-c++ is not complaining, and how can I solve it in gcc without a radical change to my code?