copy constructor not working


When i run this code, I keep getting

Copy constructor of class Book may not have a parameter of type Book on line 34.

but when i make Book as a reference it works, why ?


Book( Book & orig) : author_(orig.author_), isbn_(orig.isbn_), price_(orig.price_), title_(orig.title_) {}




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

#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)  {}


	Book( Book orig) : author_(orig.author_), isbn_(orig.isbn_), price_(orig.price_), title_(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);


};

but when i make Book as a reference it works, why ?

Because when you pass by value a copy needs to be made. So to avoid this unwanted copy you need to pass by reference.

A copy constructor is called when you initialize an object with an existing object and when the object is passed by value as a function parameter.
1
2
3
4
5
6
7
T a;
T b = a; //Copy construction.
T c(a); //Copy construction.

void f(T);

f(a); //Copy construction. 
If you could define the copy constructor to accept an object by value, then you would be able to define a function that always recurses infinitely, since calling the copy constructor would require calling the copy constructor to construct the parameters for the copy constructor. It would be pointless to allow this.
so do i always need to have the reference sign & for the copy constructor :

Book( Book & orig) : author_(orig.author_), isbn_(orig.isbn_), price_(orig.price_), title_(orig.title_) {}

but when i make Book as a reference it works, why ?

Because when you pass by value a copy needs to be made. So to avoid this unwanted copy you need to pass by reference.


and what is the problem with making a copy ? I mean I am just making a copy of a Book object , I am not invoking the copy constructor by any means ?
Last edited on
Please read the reply by helios, the last paragraph in particular.

Your post is not helpful a all, maybe if you can elaborate a little bit more what is in on your mind. I have read it , but i don't get why there would be an infinite recursion.

Book( Book orig) : author_(orig.author_), isbn_(orig.isbn_), price_(orig.price_), title_(orig.title_) {}


Book orig, ==> will call the default constructor or the 4- args constructor, so why would there be an infinite recursion ?
Book orig, ==> will call the default constructor

No the compiler will try to call the copy constructor because it needs to copy the class in order to pass the class by value. When you pass by reference you avoid the need to copy the class, instead you are passing a reference (the address) of an existing instance of your class.




and what is the problem with making a copy ? I mean I am just making a copy of a Book object , I am not invoking the copy constructor by any means ?

The copy constructor is for making copies. If you're making a copy of a Book to populate the argument for the copy constructor, the copy constructor will be invoked. And since that copy constructor invocation needs a copy of the object, the copy constructor will be invoked again, and so on and so forth.
Topic archived. No new replies allowed.