派生节点类
Derived Node class
我有基类,派生类,然后是节点类,它是从派生类派生的。 通常,我会做结构节点,在那里我会有对象对象(作为节点中的数据(。但是,在派生节点类的情况下,这是没有意义的,因为已经调用了派生的构造函数。所以,我的问题是 - 如何将数据分配给节点?
class Base
{
payload;
};
class Derived : public Base
{
payload;
};
class Node : public Derived
{
Node(Object & object);
/*
Previously I'd have struct with Object object_, and I
would do object = object_ (which would call an overload to
make a deep copy). But, in this case we already have an instance
of Derived, and making another one doesn't seem right.
So, my question is to how access that instance to assign
passed object to it? Or am I doing something completely stupid?
*/
}
您可以type conversion
(类到类(或copy constructor
。
class Node : public Derived
{
public:
Node() {
}
/** type conversion operator **/
operator Derived() {
/** code **/
}
/** copy constructor **/
Node(Node &nd_obj) : Derived(Derived d_obj){
cout<<"Node copy const called "<<endl;
cout<<"payload = "<<nd_obj.payload<<endl;
}
};
int main() {
Derived d1;
Node Nd1;
d1 = Nd1;//type conversion
Node Nd2 = d1;//copy constructor, make sure exact match is there
}
相关文章:
- 反向给定链表中的K节点
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 在派生函数中指定void*参数
- 如何通过派生类函数更改基类中的向量
- 如何委托派生类使用其父构造函数?
- Boost Graph Library,修复节点大小
- C++A*算法并不总是在路径中具有目标节点
- 如何找到2个单链表的公共节点
- 如何使用单独文件中的派生类访问友元函数对象
- 派生类销毁的最佳实践是什么
- 如何使用基类指针引用派生类成员
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何引用基类的派生类?
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 存储模板类型以强制转换回派生<T>
- 派生节点类
- 在树节点的派生类中删除dynamic_cast