通过基础 class 引用访问派生 class 成员
Access to derived class members through a base class reference
考虑一个简单的 class stDeriv,它继承自 stBase。
我很惊讶地发现我们无法通过 stBase 引用访问 stDeriv class 成员。
下面用基本的例子来说明我的观点:
#include <fstream> // std::ifstream
#include <iostream> // std::cout
using namespace std;
typedef struct stBase
{
public:
virtual ~stBase() { ; }
stBase(int iB) { iB_ = iB; }
// Copy operator
stBase &operator=(const stBase &src)
{
iB_ = src.iB_;
return *this;
}
virtual void Hello() { cout << " Hello from stBase" << endl; }
private:
int iB_ = 0;
} stBase;
typedef struct stDeriv : public stBase
{
public:
int iD_ = 0;
stDeriv(int iB) : stBase(iB), iD_(0) { ; }
virtual void Hello() { cout << " Hello from stDeriv" << endl; }
// Copy operator
stDeriv &operator=(const stDeriv &src)
{
iD_ = src.iD_;
return *this;
}
} stDeriv;
int main(int, char *[])
{
int iErr = 0;
stBase aBase(0);
stDeriv aDeriv(1);
stDeriv &rDeriv = aDeriv;
stBase &rBase = aBase;
rBase.Hello(); // OK result : "Hello from stBase"
rDeriv.Hello(); // OK result : "Hello from stDeriv"
rBase = rDeriv; // KO!!! Cannot access to aDeriv.iD_ through rBase !!!
rBase.Hello(); // KO!!! result : "Hello from stBase" !!!
return iErr;
}
为什么在“rBase = rDeriv;”之后无法通过 rBase 访问 stDeriv::iD_ ?
您不能像现在这样重新绑定引用。 rBase 已经有一个值,不能再次赋值。 why doesn't C++ allow rebinding a reference?
所以只做一个新的参考:
int main(int, char* [])
{
int iErr = 0;
stBase aBase(0);
stDeriv aDeriv(1);
stDeriv& rDeriv = aDeriv;
stBase& rBase = aBase;
rBase.Hello(); // OK result : "Hello from stBase"
rDeriv.Hello(); // OK result : "Hello from stDeriv"
// Make a new reference and all is fine
stBase& rBase2 = rDeriv;
rBase2.Hello(); // OK result : "Hello from stDeriv"
return iErr;
}
考虑一个简单的 class stDeriv,它继承自 stBase。 我很惊讶地发现我们无法通过 stBase 引用访问 stDeriv class 成员。
下面用基本的例子来说明我的观点:
#include <fstream> // std::ifstream
#include <iostream> // std::cout
using namespace std;
typedef struct stBase
{
public:
virtual ~stBase() { ; }
stBase(int iB) { iB_ = iB; }
// Copy operator
stBase &operator=(const stBase &src)
{
iB_ = src.iB_;
return *this;
}
virtual void Hello() { cout << " Hello from stBase" << endl; }
private:
int iB_ = 0;
} stBase;
typedef struct stDeriv : public stBase
{
public:
int iD_ = 0;
stDeriv(int iB) : stBase(iB), iD_(0) { ; }
virtual void Hello() { cout << " Hello from stDeriv" << endl; }
// Copy operator
stDeriv &operator=(const stDeriv &src)
{
iD_ = src.iD_;
return *this;
}
} stDeriv;
int main(int, char *[])
{
int iErr = 0;
stBase aBase(0);
stDeriv aDeriv(1);
stDeriv &rDeriv = aDeriv;
stBase &rBase = aBase;
rBase.Hello(); // OK result : "Hello from stBase"
rDeriv.Hello(); // OK result : "Hello from stDeriv"
rBase = rDeriv; // KO!!! Cannot access to aDeriv.iD_ through rBase !!!
rBase.Hello(); // KO!!! result : "Hello from stBase" !!!
return iErr;
}
为什么在“rBase = rDeriv;”之后无法通过 rBase 访问 stDeriv::iD_ ?
您不能像现在这样重新绑定引用。 rBase 已经有一个值,不能再次赋值。 why doesn't C++ allow rebinding a reference?
所以只做一个新的参考:
int main(int, char* [])
{
int iErr = 0;
stBase aBase(0);
stDeriv aDeriv(1);
stDeriv& rDeriv = aDeriv;
stBase& rBase = aBase;
rBase.Hello(); // OK result : "Hello from stBase"
rDeriv.Hello(); // OK result : "Hello from stDeriv"
// Make a new reference and all is fine
stBase& rBase2 = rDeriv;
rBase2.Hello(); // OK result : "Hello from stDeriv"
return iErr;
}