【发布时间】:2014-06-29 09:45:08
【问题描述】:
我有一个类test_d,它公开继承类test_b。 test_d 类有一个函数getValues(),我需要使用test_b 类的对象来调用它。我尝试使用dynamic_cast 和reinterpret_cast,但没有成功。有没有其他方法可以做到这一点?
class test_b {
// this is the base class
};
class test_d : public test_b {
// this is the derived class
public int getValues() const; // this is the function I need to use.
};
test_b* objB = new test_d;
dynamic_cast<test_d*>(objB)->getValues(); // this is what I am doing.
【问题讨论】:
-
这听起来像是一个严重缺陷的设计......
-
您能否详细说明“它不起作用”?
dynamic_cast看起来应该可以工作(忽略基类不应该知道其派生类型的事实。) -
为什么getValues不是基类中的虚函数?
-
public int getValues() ...不是有效的 C++ 代码,它是 Java。public之后需要一个冒号,例如public:。 -
发布一些(最少的)真实代码来重现问题。
标签: c++ class object inheritance