Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if I have a copy constructor in which the class is being passed by reference

for example

C++
Move::Move(const Move& source)
{
    data = new int;
    *data = *source.data;
}

where is the class of data attribute that I'm copying source.data within as I'm passing source by reference.

What I have tried:

i tried to debug it and review locals I've found nothing except "this" pointer
Posted
Updated 13-Sep-21 1:43am
v2

Your ultimate intention is not clear Do you merely want working code or are you investigating memory structures created by the compiler As to your example you will have to delete the destination pointer before assigning it a new value else you will have memory leakage as shown here

Move::Move(const Move& source)
{
    delete data;
    data = new int;
    *data = *source.data;
}
 
Share this answer
 
I am not sure I fully understand your question but for that code to work there has to be a declaration like this somewhere :
C++
class Move
{
public:
    Move(const Move& source);

    int * data;
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900