I've lots of classes with similar interface. The following class is a placeholder of other classes:Note that get_x() is needed to be a static function for all of the classes. Now I need to access a Derived* through a Base*:Code:class Derived : public Base { static int x; public: Derived() {x = 10;} static int get_x() {return x;} }; int Derived::x = 0;My problem is I'm failing to design such a Base class that will allow the above code in main(). b->get_x() should execute the proper derived class's get_x() it is pointing to. Since get_x() must be a static function, I cannot use a virtual method for my Base class. I tried the following versions:Code:int main() { Base *b = new Derived; cout << b->get_x() << endl; delete b; return 0; }which returns -1 always andCode:class Base { public: virtual int get_x() {return -1;} };with error in Visual C++ 2005: error C2259: 'Derived' : cannot instantiate abstract class.Code:class Base { public: virtual int get_x() = 0; };
Can you help me to design a Base class to allow the code in main()? Thanks for reading this.




Reply With Quote