Is it ever a good idea to overload copy constructor

Hi quick curiosity question.

I am writing a doubly link list program and to copy one node in a copy constructor I created an option of keeping the next and the previous pointers or not. My question is: is it ever a good practice to overload a copy constructor and would this be the correct approach to what I am doing.

Thank you,
Phil.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  
typedef struct Node
{
	Node * next;
	Node * previous;
	char character;

public:

	class NodeOperations
	{
		public:
			static void InsertBefore(Node * newNode, Node * existingAfter)
			{
				existingAfter->previous->next = newNode;
				newNode->previous = existingAfter->previous;
				newNode->next = existingAfter;
				existingAfter->previous = newNode;
			}

			static void InsertAfter(Node * newNode, Node * existingBefore)
			{
				existingBefore->next->previous = newNode;
				newNode->next = existingBefore->next;
				newNode->previous = existingBefore;
				existingBefore->next = newNode;
			}

			static Node * createNode(char c, Node * n, Node * p)
			{
				return new Node(c, n, p);
			}

			static void merge(Node& NodePrev, Node& NodeAfter)
			{
				NodeAfter.previous = &NodePrev;
				NodeAfter.next = nullptr;
				NodePrev.next = &NodeAfter;
				NodePrev.previous = nullptr;
			}
	};

	Node(char c, Node * n, Node * p) : character(c), next(n), previous(p)
	{

	}

	Node(const Node& obj, bool includePointers = false)  // <-- overloaded copy ctr here
	{
		character = obj.character;

		if (includePointers)
		{
			previous = obj.previous;
			next = obj.next;
		}
		else
		{
			previous = nullptr;
			next = nullptr;
		}

	}

} Node;




void DisplayList(Node * root)
{
	Node * it = root;

	while (it != nullptr)
	{
		std::cout << it->character << "\n";
		it = it->next;
	}
}



void DisplayFromEnd(Node * end)
{
	Node * it = end;

	while (it != nullptr)
	{
		std::cout << it->character << "\n";
		it = it->previous;
	}
}

int main()
{
	
	Node * root = Node::NodeOperations::createNode('A', nullptr, nullptr);

	Node * B = Node::NodeOperations::createNode('B', nullptr, root);
	root->next = B;
	B->previous = root;

	Node * C = Node::NodeOperations::createNode('C', nullptr, B);
	B->next = C;

	Node * E = Node::NodeOperations::createNode('E', nullptr, C);
	C->next = E;

	Node * D = Node::NodeOperations::createNode('D', nullptr, nullptr);

	Node::NodeOperations::InsertAfter(D, C);

	Node copyConstructorTest(*C);
	
	DisplayFromEnd(D);

	system("pause");
}
Last edited on
void foo(Node asdf);
¿how would you call that function so that `includePointers' is true?


> is it ever a good practice to overload a copy constructor
Either you need to, or you not
http://en.cppreference.com/w/cpp/language/rule_of_three (read rule of zero)
¿how would you call that function so that `includePointers' is true?
1
2
Node * C = Node::NodeOperations::createNode('C', nullptr, B);
Node copyConstructorTest(*C,true);
you are not calling the `foo()' function
Topic archived. No new replies allowed.