| |

VerySource

 Forgot password?
 Register
Search
Author: lfq372

Why must the formal parameters of the copy constructor be set to a reference type?

[Copy link]

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-3-31 22:30:01
| Show all posts
Because passing a value will copy the copy of the passed object, what copy is used to create the copy, and the copy constructor, so ...
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 Invalid IP Address

Post time: 2020-4-1 10:45:01
| Show all posts
If it is a value parameter passing, you need to call the copy constructor to complete the parameter passing.
And you are now precisely defining the copy constructor itself. . . .
Reply

Use magic Report

0

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-4-1 17:15:02
| Show all posts
First you have to understand ordinary functions, such as demo :: fun (const demo object), if the parameter is not a reference, the copy constructor will be called once by default.

So if there is a function demo :: demo (const demo object), the copy constructor is called when the parameter is passed, and the parameter is passed again when calling itself, so it is necessary to call itself again when calling itself It's another parameter passing, so we have to call ourselves again, ... omitting infinite words below

There used to be a mountain ...
Reply

Use magic Report

0

Threads

14

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-4-3 11:30:02
| Show all posts
Oh, what if I think demo :: demo (const demo object) is a constructor, not a copy constructor?
Reply

Use magic Report

0

Threads

15

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-4-3 20:45:02
| Show all posts
jotoos
 
 Oh, what if I think demo :: demo (const demo object) is a constructor, not a copy constructor?

=====================================
  
 Oh! !! !!
Can this construction and copy construction be considered casually?
Isn't this form already constructed for copy by default?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-5-4 10:15:02
| Show all posts
Oh, it turns out, huh, learn
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-5-8 12:00:01
| Show all posts
If it is not a quote,
Then the parameter itself needs to copy an object,
The copy of the object needs to call the copy structure,
The parameter of copy construction is another object, and it is also called copy construction ...

Endless loop. . .
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-17 13:15:01
| Show all posts
Brothers:
        Help me, my brother has a question about the SPI interception network packet technology. I have intercepted the data packet and put it in the file, but I don't know how to filter the data packet. I wrote it in C++! Experts please give pointers! ! ! thank you very much! ! It’s okay to leave a contact, like qq?
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-8-1 06:15:01
| Show all posts
I once wanted to ask this question too. At that time, I was reading a book by Qian Neng, but I didn’t write about this reason. Later, I read a book by a foreigner.

If you pass a value without passing a reference, the compiler will report a memory overflow,
The reason is: If the parameter is passed by value, you need to create a copy of the value. How to create a copy?? Use the copy constructor, but the original function itself is a copy constructor, so it calls itself, so, Form a recursive call, causing all memory to be exhausted
If it is a quote, then you can not make a copy...

It seems that I am a little bit faster than the host...I learned later, hehe!! Come on together!!
Reply

Use magic Report

1

Threads

39

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-8-1 21:00:01
| Show all posts
It is a logical error.

Parameter passing is by value, not by reference, which means that it needs to be copied once.
Copying needs to call the copy function.
The copy function needs to be passed by value, and it needs to be copied again.

This keeps looping until the memory overflows.

Therefore, the copy function should use a reference parameter that does not produce a copy operation.

demo::demo(const demo&object)
{
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list