0

I have read the following SO questions:

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?

Community
  • 1
  • 1
Itamar Katz
  • 9,544
  • 5
  • 42
  • 74

1 Answers1

0

Ok an answer that was given (and deleted before I got the chance to comment/accept) made me understand how simple the solution is (or in other words, how stupid my question was :) )

All I have to do is separate the declaration from the implementation (it was all in a single h file - bad coding, I know, it was a permanent 'temporary' solution)

struct Base {
    static void f ();
    static void g ();
};

struct Derived : public Base {
    static void f();
};

int main() {
        Derived::g ();
        return 0;
}

void Base::f () {}

void Base::g () { Derived::f(); } 

void Derived::f() {}
Itamar Katz
  • 9,544
  • 5
  • 42
  • 74
  • I deleted the answer because I am pretty much sure the code in Q won't compile without any compiler extensions.I don't know why it would compile in MSVC, perhaps a compiler extension. – Alok Save May 17 '12 at 10:09
  • I think generally MSVC is less strict than GCC (it's not the first time I encounter a code which compile in MSCV but not in GCC). Anyway thanks, your answer made me think a little bit and find the trivial solution... – Itamar Katz May 17 '12 at 10:24