Plz help,Confusion about copy constructor

Can anyone please explain to me what's copy constructor and how does it work(explain its syntax plz)? i am not sure whether i am understanding it correctly. Does it only run after an assignment operator is used?

Thank you very much

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
  #include<iostream>
using namespace std;

class Student { 
    public:   
    int studentID;   
    char *name;   
    Student() {     
        studentID = 0;     
        name = "";   }   
        Student(Student &o) {    
            studentID = o.studentID;     
            name = strdup(o.name);   
            } 
            }; 
            
            
int main() {   
    Student student1;   
    student1.studentID = 98;   
    char n[] = "foo";   
    student1.name = n;   
    Student student2 = student1;   
    student2.name[0] = 'b';   
    cout << student1.name; // foo 
    } 
A copy constructor executes when you create a new instance of a class by copying an existing instance, so either:

Student student2 = student1;

or

Student student2(student1);

The syntax is the same as the syntax of any constructor that takes an argument. In this case, the argument is an object of the same class.

I'm sure any C++ textbook will explain the syntax of writing constructors, so there's no point me duplicating that information here.
A copy constructor initializes the object with the contents of another existing object (hence the name "copy constructor" because the new object will be a copy of the existing one).

The copy constructor's signature is:

MyClass(const MyClass &); // almost the same as yours, but you omitted the `const'

Here's a Whine class example, which should make things easier to follow.

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

class Whine
{
public:

    Whine()
    {
        std::clog << "Default constructor for " << this << '\n';
    }

    Whine(const Whine &)
    {
        std::clog << "Copy constructor for " << this << '\n';
    }

    Whine & operator = (const Whine &)
    {
        std::clog << "Copy assignment operator for " << this << '\n';
        return *this;
    }

    ~Whine()
    {
//      std::clog << "Destructor for " << this << '\n';
    }
};

int main()
{
    Whine a;        // default constructor
    Whine b = a;    // copy constructor, C style syntax
    Whine c(a);     // copy constructor, C++ style syntax
    c = b;          // copy assignment operator
}

Default constructor for 0x28ff0f
Copy constructor for 0x28ff0e
Copy constructor for 0x28ff0d
Copy assignment operator for 0x28ff0d
So how is a custom copy constructor different from the default one?
Usually, the compiler will generate a default copy constructor for you, which does a member-wise shallow copy (it copies every member/data field).

This becomes a problem when the object being copied contains pointers, as the copied pointer(s) will point to the same memory address. In such a case, it's necessary for you to write a copy constructor.

Here's a stupid example of this. Let's say we have two people named Steve and Bob respectively. Steve has owns a red house, and bob a blue one. Bob wants to paint his house red, like steve's house.

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
#include <windows.h>
#include <iostream>
#include <string>

class Person;

class House {
public :
	House() {owner = NULL; color[0]=color[1]=color[2]=0;}
	__int8 color[3];

	void whoOwnsMe();
	void paint(__int8 R, __int8 G, __int8 B) {color[0]=R; color[1]=G; color[2]=B;}


	Person* owner;

};

class Person {
public :
	Person(std::string string) {name=string;}

	std::string name;
	House house;

};

void House::whoOwnsMe() {
	std::cout << "My owner is " << owner->name << "!" << std::endl;
}

int main(int argc, char* argv[]) {
	Person steve("Steve");
	Person bob("Bob");

	steve.house.owner = &steve;
	bob.house.owner = &bob;

	steve.house.whoOwnsMe();
	bob.house.whoOwnsMe();

	steve.house.paint(255, 0, 0);//steve has a red house
	bob.house.paint(0, 0, 255);//bob has a blue house

	bob.house = steve.house;//bob also wants a red house

	steve.house.whoOwnsMe();
	bob.house.whoOwnsMe();//steve has two houses

	std::cin.get();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.