C++ 复制使用列表类的堆栈类的构造函数

C++ 复制使用列表类的堆栈类的构造函数,c++,class,templates,C++,Class,Templates,如何为具有私有成员(也是另一个模板对象)的模板类调用副本构造函数 我正在构建一个stack类,它使用list类来构建堆栈。 list类有一个复制构造函数, 所以我想复制stack1=stack2 如何在堆栈复制构造函数的复制构造函数中调用它 最后一段代码是stack类的复制构造函数,我正在尝试复制私有成员列表\u 3 myData 当我执行myData=src.myData;//它复制相同的地址,不提供新对象 template <class ItemType>

如何为具有私有成员(也是另一个模板对象)的模板类调用副本构造函数

我正在构建一个stack类,它使用list类来构建堆栈。 list类有一个复制构造函数, 所以我想复制stack1=stack2 如何在堆栈复制构造函数的复制构造函数中调用它

最后一段代码是stack类的复制构造函数,我正在尝试复制私有成员列表\u 3 myData

当我执行myData=src.myData;//它复制相同的地址,不提供新对象

template <class ItemType>
            class List_3
            {

            public:


                typedef size_t size_type;

                List_3();

                List_3(const List_3 & src);

                ~List_3();

                void insert(const ItemType & item);

                void remove(); 

                void reset();

                bool advance();

                bool isEmpty() const;

                bool atEOL() const;

                bool isFull() const;

                ItemType getCurrent() const;


            private:

                struct Node {
                    ItemType value;
                    Node* next;
                    Node* previous;
                };

                Node* head;
                Node* tail;
                Node* cursor;

            };

    //Copy Constructor for List class**
    template<class ItemType>
    List_3<ItemType>::List_3(const List_3<ItemType> & src)
    {
        //Copy constructor
        head = NULL;
        tail = NULL;
        cursor = NULL;
        Node *tempCursor = new Node;
        tempCursor = src.head; // copying the original list from head to tail
        if (!src.isEmpty()) {   //if the src list is not empty start copy process
            while (tempCursor != NULL)
            {
                insert(tempCursor->value);
                cursor = NULL;
                tempCursor = tempCursor->next;  //Move to the next item in the list use previous if copying from tail
            }
            reset();                            //Reset the cursor
        }



    }


    **//============================================================================**

        template<class ItemType>
            class Stack_3 {
            public:

                typedef int size_type;

                Stack_3();

                //Copy constructor
                Stack_3(const Stack_3 & src);

                void makeEmpty();

                bool isEmpty() const;

                bool isFull() const;

                void push(const ItemType &);

                ItemType pop();

            private:
                List_3<ItemType> myData;

            };

    **//Copy Constructor for Stack Class**
    template<class ItemType>
    Stack_3358<ItemType>::Stack_3358(const Stack_3358<ItemType> & src)
    {

        myData = src.myData;

    }
模板
课程表3
{
公众:
typedef size_t size_type;
列表3();
清单3(施工清单3和src);
~List_3();
无效插入(常量项目类型和项目);
无效删除();
无效重置();
bool advance();
bool isEmpty()常量;
bool atEOL()常量;
bool isFull()常量;
ItemType getCurrent()常量;
私人:
结构节点{
项目类型值;
节点*下一步;
节点*先前;
};
节点*头;
节点*尾部;
节点*光标;
};
//复制列表类的构造函数**
样板
列表3::列表3(常量列表3和src)
{
//复制构造函数
head=NULL;
tail=NULL;
游标=空;
Node*tempCursor=新节点;
tempCursor=src.head;//从头到尾复制原始列表
如果(!src.isEmpty()){//如果src列表不是空的,则启动复制进程
while(tempCursor!=NULL)
{
插入(临时光标->值);
游标=空;
tempCursor=tempCursor->next;//移动到列表中的下一项如果从尾部复制,请使用上一项
}
reset();//重置光标
}
}
**//============================================================================**
样板
类栈3{
公众:
typedef int size_type;
堆栈_3();
//复制构造函数
堆栈_3(常量堆栈_3和src);
void makeEmpty();
bool isEmpty()常量;
bool isFull()常量;
无效推送(const ItemType&);
ItemType pop();
私人:
列出我的数据;
};
**//堆栈类的复制构造函数**
样板
堆栈_3358::堆栈_3358(常量堆栈_3358和src)
{
myData=src.myData;
}

你不需要调用构造函数。当您创建相应类的对象时,构造函数会自动运行

您需要一个初始值设定项列表来实现这一点

Stack_3358<ItemType>::Stack_3358(const Stack_3358<ItemType> & src) 
                     : myData(src.myData) // this calls List_3 copy constructor
{

}
Stack_3358::Stack_3358(const Stack_3358&src)
:myData(src.myData)//此函数调用列表\u 3复制构造函数
{
}

myData=src.myData将使用复制指定的操作符,除非复制赋值操作符被重载,否则将使用相同的地址。< / P>在您最喜欢的C++教科书中阅读“构造函数初始化列表”。它是这样的:
Stack_3358::Stack_3358(const Stack_3358&src):myData(src.myData){…}
。话虽如此,由于
List_3
需要一个用户定义的复制构造函数,它也很有可能需要一个用户定义的复制赋值操作符。这是可行的,我以为我重载了=操作符,但我再次检查,没有。
Stack_3358<ItemType>::Stack_3358(const Stack_3358<ItemType> & src) 
                     : myData(src.myData) // this calls List_3 copy constructor
{

}