With the following code I want accomplish two different values for intA, but I get:
clsA a=1? 1
clsB a=2? 1
//Code
#include <iostream>
using namespace std;
class clsA {
static const int intA = 1;
public:
virtual int get_a() { return intA; }
};
class clsB: public clsA {
static const int intA = 2;
};
int main() {
clsA a; clsB b;
cout << "clsA intA = " << a.get_a() << endl;
cout << "clsB intA = " << b.get_a() << endl;
}
How can I get intA=1 for object a and intA=2 for object b?
Thanks for the reaction,
André
Try this:
The reason the other didn’t work is that there literally existed no method clsB::get_a(). There only existed a vtable entry to which such a method might have been attached. The above attaches the method.