としてはすでに二つの別々の変数を作成している、と述べました精密:
#include <iostream>
class A
{
protected:
static int x;
public:
A() { x = 7; }
};
int A::x = 22;
class B:A
{
static const int x = 42;
public:
int a_x() const { return A::x; }
int b_x() const { return B::x; }
int my_x() const { return x; } // here we get the more local variable, that is B::x.
};
int main()
{
B b;
std::cout << "A::x = " << b.a_x() << std::endl;
std::cout << "B::x = " << b.b_x() << std::endl;
std::cout << "b's x = " << b.my_x() << std::endl;
std::cin.ignore();
return 0;
}
出力:誰かがアクセスが制限される場合がありますことを述べ
A::x = 7
B::x = 42
b's x = 42
アクセシビリティ:基本変数をprivateにすることで、子クラスにアクセスできなくなります。 ただし、変数を保護またはパブリックにする必要がある場合は、明示的なアクセス方法を使用するか、今説明したローカルスコープのルールに頼ってください。あなたは静的変数を使用している場合は、明示的に言及するのは良い考えかもしれません
lol私は、この質問が、私たちよりも速く、早くコンパイラによって返答されていると思いました – kizzx2
コンパイラはありませんか? – ralphtheninja