I want each class to have its own static code, which can be requested from each object. I am thinking of this, but it doesn't seem to work:
#include <iostream>
class Parent {
protected:
static int code;
public:
int getCode();
};
int Parent::code = 10;
int Parent::getCode() {
return code;
}
class Child : public Parent {
protected:
static int code;
};
int Child::code = 20;
int main() {
Child c;
Parent p;
std::cout << c.getCode() << "\n";
std::cout << p.getCode() << "\n";
return 0;
}
It outputs:
10
10
yet I expect
20
10