两阶段查找:我可以避免“代码膨胀”吗?
两阶段查找问题: 是否有更综合的方法来编写此代码,即避免所有这些 using
指令? 像 using CBase
这样的东西是我想要的,但它不被接受。
#include <iostream>
template <typename T>
class CBase
{
protected:
int a, b, c, d; // many more...
public:
CBase() {
a = 123; c = 0;
}
};
template <typename T>
class CDer : public CBase<T>
{
// using CBase<T>; // error, but this is what I would like
using CBase<T>::a;
using CBase<T>::b;
//...
public:
CDer() {
std::cout << a << this->c;
}
};
int main()
{
CDer<int> cd;
}
在我的真实代码中,有更多的成员变量/函数,我想知道是否可以以某种方式编写更短的代码。
当然,使用 this->c
语法并不能解决问题...
谢谢!
海湾合作委员会4.1 macOS X 10.6
Two-phase lookup question:
Is there a more synthetic way to write this code, i.e. avoiding all those using
directives?
Something like using CBase<T>;
is what I would like, but it is not accepted.
#include <iostream>
template <typename T>
class CBase
{
protected:
int a, b, c, d; // many more...
public:
CBase() {
a = 123; c = 0;
}
};
template <typename T>
class CDer : public CBase<T>
{
// using CBase<T>; // error, but this is what I would like
using CBase<T>::a;
using CBase<T>::b;
//...
public:
CDer() {
std::cout << a << this->c;
}
};
int main()
{
CDer<int> cd;
}
In my real code there are many more member variables/functions, and I was wondering if it is possible to write shorter code in some way.
Of course, using the this->c
syntax does not solve the problem...
Thank's!
gcc 4.1
MacOS X 10.6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我减少了测试用例,然后考虑三个选项
选项 1
选项 2
选项 3
I reduced the testcase and then consider three options
Option 1
Option 2
Option 3
看起来大多数变量都没有被参数化。
CBase
是全部使用它们,还是只使用一个
?如果没有,请将它们移至CDer
的新非模板库中。或者,将它们全部打包到 POD 结构中,然后使用 CBase::m_ints;。
高开销解决方案:非模板化
虚拟
基础。不确定,但值得一试:将
CDer
的定义嵌套在CBase
中,然后将typedef
放入命名空间范围内。It doesn't look like most of those variables are parameterized. Does
CBase
use them all, or justa
? If not, move them into a new non-template base ofCDer
.Or, pack them all into a POD struct and then
using CBase<T>::m_ints;
.High overhead solution: non-templated
virtual
base.Not sure but worth a try: nest the definition of
CDer
insideCBase
and thentypedef
it into namespace scope.