Reputation: 223
I have a cpp class like this:
class A{
protected:
static const int _a = 0, _b = 0, _c = 0;
std::string _data;
public:
void myMethod(); //method that changes _data based on the value of _a, _b and _c
};
If i want to create let's say:
Class B : public A{};
How do I change the values of _a
, _b
and _c
in order to change the behavior of myMethod
? Even if I declare them again, myMethod
will still use the values form class A
instead of class B
.
Do I need to override the entire myMethod
function if I want to change those 3 numbers?
EDIT: myMethod()
is public
, not private
.
Upvotes: 2
Views: 2699
Reputation: 3911
You cannot change the value of constants as the name states const
. You can only initialize them.
class A{
protected:
static const int val1, val2, val3;
public:
void myMethod();
};
const int A::val1 = 9;
const int A::val2 = 5;
const int A::val3 = 4;
Upvotes: 1
Reputation: 64895
You can't directly change the const
static members, but perhaps what you want is virtual
getA(), getB(), getC()
methods.
Then your A::myMethod()
implementation uses the getters rather than directly access to the static members.
In your B
class, you can override the get
methods to return different values (perhaps read from newly declared statics or whatever makes sense), and then A::myMethod()
will automatically pick them up.
Upvotes: 4