copy constructors

Hi, I am trying to figure out how copy constructors work and the code inside it. I think I understand what is happening but was hoping someone could just confirm if I am right or not, I am sort of new to allocating on the heap, and pointers and references in general. Code is below, copy constructor first, then the rest of the code if needed.

So what i think is happening in the copy constructor is :
1. the object is passed in by reference, so it has the address of the object (the object iself, not a copy).
2. Then a new int is created on a heap which has a pointer pointing at it called ptr.
3. Then, what the value ptr is pointing at (the int on the heap) is changed to what is being stored at for the location of the ptr object of the original object that was passed in

1
2
3
4
5
6
  Line::Line(const Line &obj) {
   cout << "Copy constructor allocating ptr." << endl;
   ptr = new int;
   *ptr = *obj.ptr; // copy the value
}


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
#include <iostream>

using namespace std;

class Line {

   public:
      int getLength( void );
      Line( int len );             // simple constructor
      Line( const Line &obj);  // copy constructor
      ~Line();                     // destructor

   private:
      int *ptr;
};

// Member functions definitions including constructor
Line::Line(int len) {
   cout << "Normal constructor allocating ptr" << endl;
   
   // allocate memory for the pointer;
   ptr = new int;
   *ptr = len;
}

Line::Line(const Line &obj) {
   cout << "Copy constructor allocating ptr." << endl;
   ptr = new int;
   *ptr = *obj.ptr; // copy the value
}

Line::~Line(void) {
   cout << "Freeing memory!" << endl;
   delete ptr;
}

int Line::getLength( void ) {
   return *ptr;
}

void display(Line obj) {
   cout << "Length of line : " << obj.getLength() <<endl;
}

// Main function for the program
int main() {
   Line line(10);

   display(line);

   return 0;
}
yes, that is pretty much correct.
a reference is not a pointer, though it acts very much like one. It may or may not be passing the original by an address (there are times when it will actually pass the variable directly, no pointers/addresses actually involved). Keep in mind that & has 2 meanings, reference and address of. A quick rule of thumb: If its on the left or a parameter, its a reference:
int &x = y;
if its on the right its address of:
int *x = &y;

This is a very tiny correction to your comment but knowing the difference will help you understand things. It has no effect on your understanding of the copy ctor, but your understanding of references.
Last edited on
Thank you, I will keep that I mind, I didnt realise the left / right hand side thing, so very helpful to keep in mind. Thanks for answering :)
A more precise way of looking at it is:

- if it's being used when declaring the type of a variable, it's a reference:

1
2
3
4
5
6
int &x = y;  // x is a reference

void MyFunc(int& x, const std::string& y)
{
  // x and y are references
}


- if it's being used as a unary operator, it's "address of":

1
2
3
int x = 3;

int* y = &x;  // y stores the address of x 
Last edited on
There is also rvalue reference, &&. See https://en.cppreference.com/w/cpp/language/reference

And then there are ref-qualifiers. See https://en.cppreference.com/w/cpp/language/member_functions
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
struct S {
    void f() & { std::cout << "lvalue\n"; }
    void f() &&{ std::cout << "rvalue\n"; }
};
 
int main(){
    S s;
    s.f();            // prints "lvalue"
    std::move(s).f(); // prints "rvalue"
    S().f();          // prints "rvalue"
}


The latter page describes also const-qualified member functions.
1
2
3
4
5
6
7
8
9
10
struct S {
    void f() { std::cout << "non\n"; }
    void g() const { std::cout << "ok\n"; }
};
 
int main(){
    const S s;
    s.f();      // error: can't call non-const func on const object
    s.g();      // prints "ok"
}

Last edited on
Topic archived. No new replies allowed.