c++ - 从派生类访问基类的 protected 数据成员

标签 c++ inheritance derived-class data-members

我有一个基类和派生类。我需要在派生类中访问基类的 protected 成员。但是,Eclipse 不允许我访问数据成员,就好像它是派生类的成员一样,而不关心它是继承的。我该怎么做?

class BaseClass {
protected:
static int a;
int b;
}


class DerivedClass: public BaseClass {    
void SomeMethod {    
a=10; // cannot resolve symbol
b=10; // cannot resolve symbol
BaseClass::a=10; //does not complain
BaseClass::b=10; //does not complain    
}
}

最佳答案

我不能完全理解你的问题,但修复了语法错误,下面应该可以工作:

class BaseClass {
protected:
static int a;
int b;
}; // <-- Missing semicolon

int BaseClass::a = 0; // Define static member

class DerivedClass: public BaseClass {    
void SomeMethod() { // <-- Missing ()
a=10;
b=10; 
}
};// <-- Missing semicolon

关于c++ - 从派生类访问基类的 protected 数据成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15057249/

相关文章:

javascript - 在对象字面量中覆盖函数

c++ - 调用构造函数时出错

c# - 使用 C++ 中的 COM 对象,在 C#.NET 中返回对象 []

Python - 为什么不继承属性?

ios - 泛型类型约束与继承

c++ - 通过 "a pointer of the base class"访问未在基类中声明的子类的方法或属性(动态)

c++ - 在 Base* 数据结构中格式化和使用派生类的正确方法是什么?

c++ - 函数指针的分支预测

c++ - 可以 operator= 可能不是成员吗?

c++ - 32位Linux中signed long long数据类型的最大大小是多少