在 c++ 中不使用虚函数从模板化派生类访问方法?
我该如何解决这个问题?我显然无法将 value() 方法设为虚拟,因为我事先不知道它是什么类型,并且在从 b 访问该方法时可能不知道这一点:
class Base
{
public:
Base() { }
virtual ~Base() { }
private:
int m_anotherVariable;
};
template <typename T>
class Derived : public Base
{
public:
Derived(T value) : m_value(value) { }
~Derived() { }
T value() { return m_value; }
void setValue(T value) { m_value = value; }
private:
T m_value;
};
int main()
{
Base* b = new Derived<int>(5);
int v = b->value();
return 0;
}
编译错误:
error: 'class Base' has no member named 'value'
How do I get around this? I clearly cannot make the value() method virtual as I won't know what type it is beforehand, and may not know this when accessing the method from b:
class Base
{
public:
Base() { }
virtual ~Base() { }
private:
int m_anotherVariable;
};
template <typename T>
class Derived : public Base
{
public:
Derived(T value) : m_value(value) { }
~Derived() { }
T value() { return m_value; }
void setValue(T value) { m_value = value; }
private:
T m_value;
};
int main()
{
Base* b = new Derived<int>(5);
int v = b->value();
return 0;
}
Compilation errors:
error: 'class Base' has no member named 'value'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
此语句:
变量“b”被视为 Derived的对象。
所以告诉编译器:
注意:如果 b 不是 Derived;转换结果为 NULL。
因此,以下内容可能会更安全:
或者,通过使用引用,您会抛出 bad_cast 异常:
或者为了友好和晦涩,我们可以用一行来做。
但我认为你可以通过 boost::any 实现你想要做的事情
This statement:
The variable 'b' is being trated like it is an object of Derived<int>.
So tell the compiler:
Note: If b is not a Derived<int> the result of the cast is NULL.
So the following would probably be safer:
Alternatively by using references you get a bad_cast exception thrown:
Or to be nice and obscrure we can do it one line.
But I think you can achieve what you are trying to do with the boost::any
如果您提出这个问题,我认为您的设计可能存在一些问题(根据我的设计经验)。
但是,有一些解决方法:
但这里真正的问题是你想在这里描述什么? Base、Derived 和 value() 的含义是什么。问自己这些问题,你可能不需要这些答案......
I think there may be some problems with your design if you ask this question (from my experience, with my designs).
However, there are some workarounds:
But the real question here is what do you try to describe here? What is the meaning of Base, Derived and value(). Ask yourself these question and you may not need these answers...
一些解决方案:
另一个解决方案:
Some solution:
Another solution:
在这种情况下,Base 将需要了解模板类型,因此 b->value 返回 T。
我建议将模板添加到 Base,然后将 value 设置为 Base 上的虚拟函数
In this case Base will need to know about the template type so b->value returns a T.
I would suggest adding the template to Base and then make value a virtual function on Base
您可以将指针强制转换为
Derived
:当然,在实际代码中,您必须添加检查以确保
dynamic_cast
不会失败。You could cast your pointer to
Derived
:Of course in real code, you have to add checking to make sure that
dynamic_cast
didn't fail.为什么不制作基于 Base 模板的类呢?那么您就可以拥有作为虚拟会员的价值。
或者您可以将 b 向下转型为 Derived。
Why don't make Base template-based class too? Then you can have value as a virtual member.
Or you can downcast b to Derived.
看起来您想要动态多态性,但只使用模板的“静态多态性”。
如果您想要动态多态性,您确实需要基类中的虚拟成员(以及虚拟析构函数)或 down_cast (如果您没有通用接口)。
如果没有,请删除虚拟析构函数并仅使用指向派生类型的指针或实例。
关于 Base 作为模板:
它将阻止您拥有用于动态多态性的单个基类,如 Base<;整数>且基数<其他>将会不兼容。
It looks like you want dynamic polymorphism but using only the "static polymorphism" of templates.
If you want dynamic polymorphism you do need virtual members in your base class ( along with your virtual destructor ) or the down_cast if you do not have a common interface.
If not, remove the virtual destructor and use only pointers to or instances of derived types.
About Base as a template :
It will prevent you from having a single base class for dynamic polymorphism as Base< int > and Base< other > will be incompatibles.
处理此问题的一种方法是通过访问者模式。基本思想是,在类的层次结构中实现一个接受访问者的
onNode
函数。然后,您编写执行您想要的操作的特定访问者。就你而言,你最终会得到:One way of handling this is via the visitor pattern. The basic idea is that in your hierarchy of classes you implement an
onNode
function that accepts a visitor. Then, you write specific visitors that do what you want. In your case, you'd end up with: