我有一段cpp的階級是這樣的值:C++重寫繼承的靜態常量
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
};
如果我想創建讓我們說:
Class B : public A{};
如何更改的_a
值,_b
和_c
爲了改變myMethod
的行爲?即使我再次聲明它們,myMethod
仍將使用值class A
而不是class B
。
如果我想更改這3個數字,是否需要覆蓋整個myMethod
函數?
編輯:myMethod()
是public
,而不是private
。
如果您明確調用B :: myMethod(),行爲是否會改變? – xyious
這些值是常量,因此它們不能更改。 –
你不能改變它們,因爲它們是'const'。因此 - 它們只能被初始化。 –