using the default keyword



if i have defined a class called Book, and i write the following 2 version of the copy assignment operator , are they equivalent ?

1
2
3
4
5
6
7
8
9
10
11
12

Let's say I have defined these 2 assignment operators within the book class:

is this line 

Book& operator = (Book const& rhs);

equivalent to:

Book& operator = (Book const& rhs) = default;

   
I don't quite understand, ¿you've got something like this?
1
2
3
4
5
class foo{
	public:
		foo& operator=(const foo &);
		foo& operator=(const foo &) = default;
};
that doesn't compile
foo.cpp|4 col 8| error: ‘foo& foo::operator=(const foo&)’ cannot be overloaded
foo.cpp|3 col 8| error: with ‘foo& foo::operator=(const foo&)’
If you're asking if line 6 and line 10 are the same, the answer is no.

Line 10 says the compiler will provide a standard shallow copy assignment.

Line 6 says you're going to provide an implementation of the assignment operator which may or may not be the same as the default implementation that the compiler provides. If you're providing a non-standard assignment operator, the assumption is that the default assignment operator is not adequate for your needs.


I got the same linking error for both versisons:

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: class Book & __thiscall Book::operator=(class Book const &)" (??4Book@@QAEAAV0@ABV0@@Z) referenced in function "public: void __thiscall equalassignment_operator::test_method(void)" (?test_method@equalassignment_operator@@QAEXXZ) midtermbooks C:\Users\myname\source\repos\OO\midtermbooks\midtermbooks\Source1.obj 1
Best is to show us your code, otherwise we can just guess.
Are you sure that you need the operator= ?
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228


book.hpp: 

header:

#pragma once
#include <string>

using namespace std; 

class Book {

private:

	string author_ = "unknown";
	int isbn_ = 0;
	int price_ = 0;
	string title_ = "unknown";


public:


	// once you implement the 4 - args constructor, the synthesized default constructor gets overridden
	// so you need to implement a default constructor by urself
	// default constructor

	Book() {
		author_ = "unknown";
		isbn_ = 0;
		price_ = 0;
		title_ = "unknown"; 
	}

	// 4- args constructor 
	Book(string author, int isbn, int price, string title) : author_(author), isbn_(isbn), price_(price), title_(title)  {}

	// does not work , need to make Book as a reference
	//Book( Book orig) : author_(orig.author_), isbn_(orig.isbn_), price_(orig.price_), title_(orig.title_) {}

	// notice how there is a reference sign infront of Book
	Book(Book const&  orig) : author_(orig.author_), isbn_(orig.isbn_), price_(orig.price_), title_(orig.title_) {}


	// move constructor 
	Book(Book && orig) : author_(std::move(orig.author_)), isbn_(std::move(orig.isbn_)), price_(std::move(orig.price_)), title_(std::move(orig.title_)) {}




	string getAuthor()  {

		return author_;

	}


	string getTitle()  {

		return title_;

	}


	int getIsbn()  {

		return isbn_;

	}

	int getPrice()  {

		return price_;

	}


	void setAuthor(string author) {
		author_ = author;
	}


	// Overload + operator to add two Books
	Book operator+(const Book& b) {
		Book b3;
		b3.author_ = this->author_ + b.author_;
		b3.isbn_ = this->isbn_ + b.isbn_;
		b3.price_ = this->price_ + b.price_;
		b3.title_ = this->title_ + b.title_;
		return b3;
	}

	// allows operator + access private fields inside of book 
	friend Book operator + (const Book& b1, const Book& b2);

	//friend Book operator = (const Book& b1);


	/* need to remove the comment , to make copy assignment operator work , to run the program without error
	copy assignment operator which needs to be implemented, because we have implemented a move constructor 
	/*Book& operator = (Book const& rhs) {
		
		author_ = rhs.author_;
		 isbn_ = rhs.isbn_;
		price_ = rhs.price_;
		 title_ = rhs.title_;

		return *this;
	}*/


	// trying out the default implementation of Copy Assignment Operator which results in an linking erorr
	Book& operator = (Book const& rhs) = default;


	// move assignment operator , cannot use const infront of rhs here
	//Book& operator = (Book const && rhs) => wrong , we are actually changing the value for rhs , so we cannot use const here
	Book& operator = (Book  && rhs) {

		author_ = std::move(rhs.author_);
		isbn_ = std::move(rhs.isbn_);
		price_ = std::move(rhs.price_);
		title_ = std::move(rhs.title_);

		return *this;
	}



};


Source.cpp:


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "book.hpp"

using namespace std;





bool sortbyauthor(Book lhs, Book rhs)
{
	return lhs.getAuthor() < rhs.getAuthor(); 
}


ostream& operator<<(ostream& os,   Book&  dt)
{
	os << dt.getAuthor() << '/' << dt.getIsbn() << '/' << dt.getPrice() << '/' << dt.getTitle() << endl; 
	return os;
}




Book  operator + (const Book& b1, const Book& b2) {
	Book b3;
	b3.author_ = b1.author_ + b2.author_;
	b3.isbn_ = b1.isbn_ + b2.isbn_;
	b3.price_ = b1.price_ + b2.price_;
	b3.title_ = b3.title_ + b3.title_;
	return b3;
}




int main()
{
	Book b1;

	
	b1.setAuthor("bb");

	Book b2;
	b2.setAuthor("aa");

	vector <Book> v;

	v.push_back(b1); 
	v.push_back(b2);

	cout << "before sorting by author" << endl; 

	for (auto x : v)
		cout << x ;

	sort(v.begin(), v.end(), sortbyauthor);

	cout << "after sorting by author" << endl;

	for (auto x : v)
		cout << x ; 


	cout << "after sorting by author displaying using ranged for loop " << endl;
	for(int unsigned i = 0; i < v.size(); ++i)
	{
		cout << v[i] ;
	}
	
	Book b3;
	b3 = b2 + b1; 
	cout << " b1 + b2 is " << b3 << endl; 


}












bump
Registered users can post here. Sign in or register to post.