【发布时间】:2014-11-17 14:56:24
【问题描述】:
在以下代码中,sn -p check() 无法访问名为 test 的变量:
int test; //global variable
class Base {
private:
int test; //private member of Base
public:
void getit() {
cin>>test;
}
};
class Derived: public Base {
public:
void check(){
test ++; //Increments neither global variable nor private member of Base
}
}
我在这里缺少范围呢? check() 不应该至少可以访问全局变量吗?
【问题讨论】:
-
在
check范围内,不带前缀的test指的是Base::test,因为它是私有的,所以不能访问。如果要增加全局变量,请改用::test。
标签: c++ class inheritance c++11