Bri*_*own 6 c++ inheritance protected public
继承我的代码:
#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)
{
}
Run Code Online (Sandbox Code Playgroud)
当我想访问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 ===|
Run Code Online (Sandbox Code Playgroud)
我读到受保护的成员在派生类中是私有的,所以它似乎没问题,但它不是.如何解决这个问题?
我读到受保护的成员在派生类中是私有的,所以它似乎没问题,但它不是.
这不是因为只要在类型()的对象上访问,就可以访问派生类(在本例中)protected从基类继承的数据成员A(root在本例中).在这里,您通过type ()对象访问它:BaaBaaAroot
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;
}
Run Code Online (Sandbox Code Playgroud)
根据C++ 11标准的第11.4/1段:
当非静态数据成员或非静态成员函数是其命名类的受保护成员时,将应用超出第11章中所述之外的其他访问检查(11.2).如前所述,授予对受保护成员的访问权限,因为引用发生在某个C类的朋友或成员中.如果访问要形成指向成员的指针(5.3.1),则嵌套名称说明符应表示C或从C派生的类.所有其他访问都涉及(可能是隐式的)对象表达式(5.2.5).在这种情况下,对象表达式的类应为C或从C派生的类. [ 示例:
Run Code Online (Sandbox Code Playgroud)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) }- 结束例子 ]
要解决此问题,您需要提供公共setter/getter.你已经有了一个getSize()函数,所以不要写这个:
for (int i=0; i<a.size; i++)
// ^^^^^^
Run Code Online (Sandbox Code Playgroud)
你可以这样写:
for (int i=0; i<a.getSize(); i++)
// ^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
同样,您必须提供获取/设置第n个元素值的函数array,以便您可以编写:
array[i] += a.get_at(i);
Run Code Online (Sandbox Code Playgroud)
请注意,左侧的表达式+=是正常的,因为array正在访问this(请参阅上面的C++ 11标准示例).