无法从派生类访问基类中的受保护成员
Can't access protected member in base class from derived class
下面是我的代码:
#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;
class root
{
protected :
int size;
double *array;
public :
virtual ~root() {}
virtual root* add(const root&) = 0;
virtual root* sub(const root&) = 0;
virtual istream& in(istream&, root&) = 0;
virtual int getSize() const = 0;
virtual void setSize(int);
};
class aa: public root
{
public :
aa();
aa(int);
aa(const aa&);
root* add(const root& a);
root* sub(const root& a);
istream& in(istream&, root&){}
int getSize() const;
void setSize(int);
};
class bb: public root
{
public:
bb() { }
bb(const bb& b) { }
root* add(const root& a);
root* sub(const root& a);
istream& in(istream&, root&){}
int getSize() const{}
void setSize(int){}
};
aa::aa()
{
size = 0;
array = NULL;
}
aa::aa(int nsize)
{
size = nsize;
array = new double[size+1];
for(int i=0; i<size; i++)
array[i] = 0;
}
root* aa::add(const root& a)
{
for (int i=0; i<a.size; i++)
array[i] += a.array[i];
return *this;
}
root* aa::sub(const root& a)
{
}
int aa::getSize() const
{
return size;
}
void aa::setSize(int nsize)
{
size = nsize;
array = new double[size+1];
for(int i=0; i<size; i++)
array[i] = 0;
}
root* bb::add(const root& a)
{
return new bb();
}
root* bb::sub(const root& a)
{
}
int main(int argc, char **argv)
{
}
当我想访问size
或array
在派生类,我只是不能,因为我的编译器说:
/home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root* aa::add(const root&)’:|
/home/brian/Desktop/Temp/Untitled2.cpp|10|error: ‘int root::size’ is protected|
/home/brian/Desktop/Temp/Untitled2.cpp|66|error: within this context|
/home/brian/Desktop/Temp/Untitled2.cpp|11|error: ‘double* root::array’ is protected|
/home/brian/Desktop/Temp/Untitled2.cpp|67|error: within this context|
/home/brian/Desktop/Temp/Untitled2.cpp|68|error: cannot convert ‘aa’ to ‘root*’ in return|
||=== Build finished: 5 errors, 0 warnings ===|
我读到受保护的成员在派生类中是私有的,所以它似乎是好的,但它不是。如何解决这个问题?
这并不是因为我读到受保护的成员在派生类中是私有的,所以它似乎是可以的,但它不是。
B
(aa
)从基类A
(在本例中为root
)继承的protected
数据成员可以访问,只要它在类型为B
(aa
)的对象上被访问。这里,您通过类型为A
(root
)的对象访问它:
root* aa::add(const root& a)
{
for (int i=0; i<a.size; i++)
// ^^^^^^
// Accessing size on an object of type `root`, not `aa`!
array[i] += a.array[i];
return *this;
}
c++ 11标准第11.4/1段:
当一个非静态数据时,在第11条前面描述的访问检查之外应用额外的访问检查成员或非静态成员函数是其命名类的受保护成员(11.2)。所述以前,授予对受保护成员的访问权限是因为引用发生在某个成员的友元或成员中类C 。如果访问要形成指向成员(5.3.1)的指针,则嵌套名称说明符应表示C或a所有其他访问都涉及(可能隐式的)对象表达式(5.2.5)。在这种情况下,对象表达式的类必须是C或从C派生的类。 [示例:
class B { protected: int i; static int j; }; class D1 : public B { }; class D2 : public B { // ... void mem(B*,D1*); }; void D2::mem(B* pb, D1* p1) { pb->i = 1; // ill-formed p1->i = 2; // ill-formed // ... i = 3; // OK (access through this) B::i = 4; // OK (access through this, qualification ignored) j = 5; // OK (because j refers to static member) B::j = 6; // OK (because B::j refers to static member) }
- end示例]
要解决这个问题,你需要提供公共setter/getter。您已经有了一个getSize()
函数,所以不要这样写:
for (int i=0; i<a.size; i++)
// ^^^^^^
你可以这样写:
for (int i=0; i<a.getSize(); i++)
// ^^^^^^^^^^^
类似地,您必须提供用于获取/设置array
的第n个元素的值的函数,以便您可以这样写:
array[i] += a.get_at(i);
注意,+=
左边的表达式是OK的,因为array
是通过this
访问的(参见上面的c++ 11标准中的例子)。
相关文章:
- C++:无法访问声明的受保护成员
- 相同的层次结构,访问基类的受保护成员时的行为不同
- 同一模板类但模板类型的受保护成员
- 派生类无法访问基类的受保护成员
- 继承期间受保护成员的皮条
- 如何将超类的受保护成员访问到其派生类. 如果已在派生类中声明了具有相同名称的函数?
- 是否可以基于私有/受保护成员分配类枚举?
- 依赖模板基础的受保护成员
- 无法从派生类型的作用域访问另一个实例的受保护成员
- 为什么我无法使用受保护/私有继承访问派生实例中基类的受保护成员?
- 将对象强制转换为派生类以访问父类的受保护成员
- 使从一个基类派生的类能够使用继承的受保护成员
- 派生类如何使用基类的受保护成员
- 访问派生类C++中的受保护成员
- 访问基类的受保护成员
- 如何访问多个继承类中的受保护成员
- 如何从另一个类的基类访问派生类中的受保护成员
- 无法访问派生类中的基本受保护成员!(在虚函数中)
- 如何通过派生类访问基类的受保护成员?
- 如果孩子不需要父母班级的所有受保护成员,这是不好的班级设计吗?