| |

VerySource

 Forgot password?
 Register
Search
View: 3809|Reply: 24

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

[Copy link]

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-7 19:30:01
| Show all posts |Read mode
Why must the formal parameters of the copy constructor be set to a reference type?
Such as:
demo :: demo (const demo&object)
{
}
and
demo :: demo (const demo object)
{
}
Can't it work?
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-3-29 10:30:02
| Show all posts
Will lead to infinite recursion
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 United States

Post time: 2020-3-29 13:15:01
| Show all posts
Because when the parameters are passed
If you adapt to passing objects instead of references, the combination of formal and actual parameters will use the copy constructor
Cause infinite recursion
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 Unknown

Post time: 2020-3-29 15:00:01
| Show all posts
Personal opinions, experts advise
Reply

Use magic Report

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-29 20:30:01
| Show all posts
I still do n’t understand what you said
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-3-29 21:00:01
| Show all posts
Infinite recursion
Reply

Use magic Report

0

Threads

15

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-3-30 13:45:01
| Show all posts
References guarantee that you pass in an object
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-3-30 15:15:01
| Show all posts
Because a copy constructor is called when taking an object as a parameter, if the copy constructor takes an object as a parameter, it will call itself.
Reply

Use magic Report

0

Threads

9

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-3-30 17:15:01
| Show all posts
Because often cited does not produce side effects. It seems straightforward
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-3-31 17:00:01
| Show all posts
Because parameters are passed by value
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