【发布时间】:2014-11-08 07:00:17
【问题描述】:
我想将基类指针转换为派生类,以便利用派生类独有的一些方法。这是一个有效的简单示例的Ideone:
template<typename A>
class Base {};
template<typename A, typename B>
class Derived : public Base<A> {
public:
void doSomething() {}
};
int main() {
Base<int>* foo = new Derived<int, double>;
static_cast<Derived<int, double>*>(foo)->doSomething();
return 0;
}
现在,问题是我的foo 实际上是模板类的成员,
template<typename A>
class Container
{
public:
Base<A>* foo;
};
而在我投射的时候,我不知道A 是什么:
int main() {
Container<int> container;
container.foo = new Derived<int, double>;
// a lot of code later...
static_cast<Derived< /* ??? */ , double>*>(container.foo)->doSomething();
return 0;
}
然后我想如果我能以某种方式将 A 的内容存储在我的基类中,这可能是可能的,例如
template<typename A>
class Base
{
public:
static type template_type = A; // made-up syntax
};
这样我可以参考它
static_cast<Derived<container.template_type, double>*>(container.foo)->doSomething();
但根据this question,不可能在 C++ 中存储类型。
如何在不知道A 的情况下实现此演员阵容?
也就是说,我如何将专门的基指针转换为专门用于附加模板参数的派生指针?用较少的技术术语来说,我只想采用 Base 指针并附加形成 Derived 指针所需的其他特化。
【问题讨论】:
-
你肯定知道 A 是什么,因为你有一个 Base 类型的对象,你正试图投射?
-
这就是我一直在想的,但这很奇怪,我看不到在我的设置中知道
A的方法。让我再看一遍,也许再稍微编辑一下以说明原因。 -
@ajclinto - 不,你是对的。我想到了。我有同样的预感,但以前没有看到。我认为我设计用于打印
Container元素的实用函数必须是通用的,这就是为什么我认为我不知道A会是什么。但是在跟踪代码之后,我意识到这个实用函数只有在特定类型A的Containers 上被调用,所以即使之前感觉不对,硬编码一个类型A实际上是正确的它会打印。逻辑有效!结束这个问题。 -
我会关闭这个问题,但不会删除它,以防有人过来想做同样的事情。解决方案:更仔细地检查您的代码,看看您是否正在尝试制作一些不应该/曾经是通用的通用内容。
标签: c++ class templates inheritance c++11