且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以在派生类中对基类的成员进行别名吗?

更新时间:2023-02-15 16:11:42

Xeo的提示工作。如果你使用的是C ++ 11,你可以这样声明别名:

Xeo's tip worked. If you are using C++ 11, you can declare the aliases like so:

int   &theInt   = Base<int>::theT;
float &theFloat = Base<float>::theT;

如果你没有C ++ 11,我想你也可以在构造函数中初始化它们:

If you don't have C++11, I think you can also initialize them in the constructor:

int   &theInt;
float &theFloat;
// ...
Derived() : theInt(Base<int>::theT), theFloat(Base<float>::theT) {
  theInt = // some default
  theFloat = // some default
}

编辑:
轻微的烦恼是,你不能初始化这些别名成员的值直到构造函数的主体(即,大括号内)。

The slight annoyance is that you can't initialize the the value of those aliased members until the main body of the constructor (ie, inside the curly braces).