c++相互依赖的类模板,指针投射派生类
c++ interdependent class templates, pointer casting derived classes
你好,我正在编写一个显示/显示不同图形并对其进行基准测试的程序。图由节点和边组成。。。所以我的问题是我有两个模板类(templates),它们是所有派生类的基础
template <class Node>
class Edge
{
public:
Edge() : startN(0), endN(0), price(0) {}
Edge(Node *startN, Node *endN, int price) : startN(startN), endN(endN), price(price)
{
startN->toEdges.push_back(this); // PROBLEM HERE
endN->fromEdges.push_back(this); // PROBLEM HERE
}
Node *startNode() const {
return startN;
}
Node *endNode() const {
return static_cast<Node *>(endN);
}
int getPrice() const {
return price;
}
void setPrice(int price) {
this->price = price;
}
private:
Node *startN;
Node *endN;
int price;
};
template<template<class> class EdgeTemplate >
class NodeBase
{
public:
NodeBase() : nodeId(0), key(0), state(UNLABELED), prev(0) {}
NodeBase(int id, int key) : nodeId(id), key(key), state(UNLABELED), prev(0) {}
void addToEdges(EdgeTemplate<NodeBase> *edge) {
toEdges.push_back(static_cast<EdgeTemplate<NodeBase> *>(edge));
}
int nodeId;
int key;
State state;
NodeBase *prev; // prevous scanned
QVector<EdgeTemplate<NodeBase> *> fromEdges; // start
QVector<EdgeTemplate<NodeBase> *> toEdges; // end
};
错误发生在另一个模板类中:
template <template<class> class EdgeTemplate, class Node>
class DijkstraAlgorithm {
...
QVector<EdgeTemplate<Node> *> Edges; // the problem for derived classes
...
};
Clang:
error: cannot initialize a parameter of type 'Edge<NodeBase<Edge> > *' with an rvalue of type 'Edge<DNode> *'
startN->addToEdges(this);
^~~~
GCC:
error: no matching function for call to 'QVector<Edge<NodeBase<Edge> >*>::push_back(Edge<DNode>* const)'
因此,据我所知,问题是派生类DNode
(class DNode : public NodeBase <Edge>
)不能存储在基类型NodeBase<Edge>
的共容器中。。。我试过选角,但没用。
有人能解释一下我做错了什么吗?我该如何解决这个问题?
在查看模板时,继承关系船根本不重要。
struct B {};
struct D : B {};
template<typename T>
struct C {};
C<B> *c = new C<D>; // error C<D> is completely different and has no relationship to C<B>
// you might as well say:
float *f = new char[50];
考虑:
template<>
struct C<B> {
int a,b,c;
int foo() { return a+b+c;}
};
template<>
struct C<D> {
std::string s;
std::string bar();
};
C<B> *c = new C<D>; // pretend it works.
c->foo(); // C<D> doesn't have a,b or c and doesn't have a method foo...
也许NodeBase应该只将边缘类型作为模板参数,而不是边缘模板。
template<typename Edge> struct NodeBase {
QVector<Edge *> fromEdges;
};
则DNode继承自NodeBase<Edge<DNode>>
也许有更好的方法,也许可以更直接地使用CRTP,但很难说不看到更多当前的设计。
相关文章:
- 为什么使用 "this" 指针调用派生成员函数?
- 如何使用基类指针引用派生类成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 如果基类包含双指针成员,则派生类的构造函数
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- 如何在工厂方法中返回指向基于基础操作系统的派生类的有效指针
- 指向基类派生类的 std::unique_ptr 的指针
- 当我在 C++ 中将派生类的指针分配给指针时,地址会更改
- 从基指针到派生的强制转换问题
- 派生类是从基类继承 v 指针并仅使用它,还是也有自己的 v 指针?
- 从纯虚拟类 (A) 派生的指针无法访问来自纯类 (B) 的重载方法
- 如何模板化堆栈分配的多态指针数组到接口,包括派生类型的相应点?
- 从基类指针派生派生类的模板类型
- 指向从指针派生类成员函数的指针,指向基类成员函数
- 访问通过指针派生的类
- 如果未使用虚拟函数,则使用基类指针派生类的任何目的
- 从基指针C++派生类的类型
- 无法从指针派生类转换为指针基类(多态性)
- C++ - 传递智能指针派生类