Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 派生节点类_C++ - Fatal编程技术网

C++ 派生节点类

C++ 派生节点类,c++,C++,我有基类、派生类,然后是从派生类派生的节点类。通常,我会使用struct Node,在这里,我会将Object对象作为节点中的数据。但是,对于派生节点类来说,这毫无意义,因为已经调用了派生节点的构造函数。所以,我的问题是-如何将数据分配给节点 class Base { payload; }; class Derived : public Base { payload; }; class Node : public Derived { Node(Object &

我有基类、派生类,然后是从派生类派生的节点类。通常,我会使用struct Node,在这里,我会将Object对象作为节点中的数据。但是,对于派生节点类来说,这毫无意义,因为已经调用了派生节点的构造函数。所以,我的问题是-如何将数据分配给节点

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?
    */
}
您可以将conversionclass键入类或复制构造函数

NodeObject&object:Derivedobject{}?
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

}