2016-11-13 9 views
-1

私はビデオや古い投稿を見てみましたが、コピーコンストラクタのコンセプトを理解することはまだ非常に難しいです。誰かが私のためにそれをクリアするだろうか?私のクラスは、この部分を実際にカバーしていませんでした。私の教授は主にコンストラクタとデストラクタに焦点を合わせました。リンクリストのコピーコンストラクタを作成して実行する方法は?

メインCPP

#include <iostream> 
#include "Header.h" 
using namespace std; 


int main() 
{ 
    node access; 

    access.getData(); 
    access.outData(); 

    system("pause"); 
    return 0; 
} 

ヘッダーファイル

#include <iostream> 

using namespace std; 

class node 
{ 
public: 
    node(); // Had to create my own default constructor because of my copy constructor. 
    node(const node &n); // This is a copy constructor. 
    ~node(); 
    void getData(); 
    void outData(); 
private: 
    int num; 
    int lCount = 0; // Counts the number of nodes, increments after each user input. 
    int *ptr; // Where the linked list will be copied into 
    node *next; 
    node *first; 
    node *temp; 
    node *point; 
}; 

node::node() 
{ 
    num = 0; 
} 

node::node(const node &n) 
{ 
    temp = first; 

    ptr = new node; 
    for (int i = 0; i < lCount; i++) 
    { 
     ptr[i] = temp->num; 
     temp = temp->next; 
    } 

} 

node::~node() // Deletes the linked list. 
{ 
    while (first != NULL) 
    { 
     node *delP = first; // Creates a pointer delP pointing to the first node. 
     first = first->next; // "Removes first node from the list and declares new first. 
     delete delP; // Deletes the node that was just removed. 
    } 
    cout << "List deleted" << endl; 
} 

void node::getData() // Simple function that creates a linked list with user input. 
{ 
    int input = 0; 
    point = new node; 
    first = point; 
    temp = point; 

    while (input != -1) 
    { 
     cout << "Enter any integer, -1 to end." << endl; 
     cin >> input; 

     if (input == -1) 
     { 
      point->next = NULL; 
      break; 
     } 
     else 
     { 
      lCount++; 
      point->num = input; 
      temp = new node; 
      point->next = temp; 
      point = temp; 
     } 
    } 
} 

void node::outData() 
{ 
    temp = first; 
    cout << "Original" << endl; 
    while (temp->next != NULL) 
    { 
     cout << temp->num << endl; 
     temp = temp->next; 
    } 

    cout << "Copied" << endl; 
    for (int i = 0; i < lCount; i++) 
    { 
     cout << ptr[i] << endl; 
    } 
} 

はこの小さなスニペットは、私は特にとのトラブルを抱えていますものです:

node::node(const node &n) 
{ 
    temp = first; 

    ptr = new node; 
    for (int i = 0; i < lCount; i++) 
    { 
     ptr[i] = temp->num; 
     temp = temp->next; 
    } 
} 
+0

https://www.tutorialspoint.com/cplusplus/cpp_copy_constructor.htm –

+1

可能[リンクリストのコピーコンストラクタを作成する]の複製(http://stackoverflow.com/questions/7811893/creating-a-copy-construct)リンクリストのために) –

+0

@JasonC私はそれを既に見てきました。私はちょうど他人のコードを見て理解していない。私は自分のコードを見て、それを私のためにクリアすることを望んでいます。 –

答えて

0

私はそれを理解しました。私ははるかに単純なコピーコンストラクタを手直ししていました。私は文法を理解するのが難しかった、すべてが非常に複雑で、見るのが圧倒的だった。

#include <iostream> 

using namespace std; 

class node 
{ 
public: 
    node(int x); // Normal Construtor 
    node(const node &cpy); // Copy Constructor 
    void change(); // Changes data value 
    void outData(); 
private: 
    int data; 
}; 

int main() 
{ 
    node var1(123); 
    var1.outData(); 
    node var2 = var1; 
    var2.outData(); 

    var2.change(); 
    var1.outData(); 
    var2.outData(); 

    system("pause"); 
    return 0; 
} 

node::node(int x) 
{ 
    data = x; 
} 

node::node(const node &cpy) 
{ 
    data = cpy.data; 
} 

void node::outData() 
{ 
    cout << data << endl; 
} 

void node::change() 
{ 
    int userIn; 
    cin >> userIn; 
    data = userIn; 
} 

出力:

123 
123 

(入力:4444)

出力:

123 
4444 
-1

コピーを呼び出すにはコンストラクタあなたがする必要があるのは、そのデータ型の変数を値渡しすることだけです。プロトタイプtype::type(const type &old)のメソッドを作成するにはどのようにコピーするかはあなた次第です。しかし、一般的には、深いコピーと呼ばれるものを実行したいと考えています。そこでは、新しいもののために新しいものすべてを作成します。例:

DoubleLinkedList(const DoubleLinkedList &other) { 
    //start with empty list 
    numberNodes = 0; 
    firstNode = nullptr; 
    lastNode = nullptr; 
    //build new list- iterate over every node 
    for (auto i = other.firstNode; i; i = i->next()) { 
     //make new node for the old nodes 
     push_back(i->data()); 
    } 
} 
+0

あなたは値渡しと言っていますが、コード内で参照渡ししています。 btw私はdownvoterではない –

+0

"other.firstNode"は正しいからコピーされている元のリストからですか? –

+0

@DannyOrangeCitrusはい –

関連する問題