when to use copy constructors

Do we need to write a copy constructor if my class has just one char pointer variable?

1
2
3
4
5
6
class Test
{
public:
	char *a;
	Test(void){ }
};


Last edited on
It is determined by how you want that the member of the class were copied.
Ok., So if I don't write a copy constructor for above class then what will happen? Will it do just shallow copy?

Please see below code
1
2
3
4
5
6
7
8
9
10
	Test testObj;
	testObj.a ="no copycon";
	Test b = testObj; //will call default copycon
	cout << testObj.a << endl; //will print no copycon
	cout << b.a<< endl;; //will print no copycon

	testObj.a = "changed text";

	cout << testObj.a << endl;// will print changed text
	cout << b.a; // still prints no copycon 


Why the last line still prints "copycon"? As per my understanding it should changed and should display the changed string i.e. "changed text".
Isn't it?
1) "changed text" has its own address.
2) each object of the class Test his its own member 'a';
3) You have changed only the member of object testObj.
Last edited on
ok thanks. But how to change above code so that I get an effect like if i change just the member of object testObj, and same change gets automatically reflected to the member of another object "b".
Define this mamber as static.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 class Test
{
public:
	static cont char *a;
	Test(void){ }
};

const char * Test::a;

Test testObj;
testObj.a ="no copycon";
Test b = testObj; //will call default copycon
cout << testObj.a << endl; //will print no copycon
cout << b.a<< endl;; //will print no copycon

testObj.a = "changed text";

cout << testObj.a << endl;// will print changed text
cout << b.a; // still prints no copycon  
U need a copy destructor implemented, that will dealocate the pointers and free the memory
1
2
3
4
5
6
7
8
9
10
11
class Test
{
public:
	char *a;
	Test(void){ }
        ~Test() 
        {
           delete(a);// memory is freed 
           a = NULL; // pointer a points to nothing
        }
};


constructors are used when you do not want to have a separate function that will initialize the object.
example:
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
#include <iostream>
using namespace std;

class Test
{
public:
	char *a;
	Test(void)
        {
           a = new char; //pointer a is initialized
        }

        ~Test() 
        {
           delete(a);// memory is freed 
           a = NULL; // pointer a points to nothing
        }
};

void main ()
{
    char b='s';
    Test testObj;
    
    *(testObj.a) = b; // operator * is used to acces the data pointed by the pointer a
    cout<<*(testObj.a)<<endl;
	system("pause");
}




or u can create a separate function

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

class Test
{
public:
	char *a;
	void Initialize(void) //insted of contructor we use a normal function
        {
           a = new char; //pointer a is initialized
        }

        ~Test() 
        {
           delete(a);// memory is freed 
           a = NULL; // pointer a points to nothing
        }
};

void main ()
{
    char b='s';
    Test testObj;
	testObj.Initialize(); //initialize the class
    
    *(testObj.a) = b; // operator * is used to acces the data pointed by the pointer a
    cout<<*(testObj.a)<<endl;
	system("pause");
}
Last edited on
Err....
1
2
3
           a = NULL; // if you set a to NULL
           delete(a);// then you are deleting NULL
           //  delete NULL does absolutely nothing. 
Moreover in the original code the pointer is assigned a string literal. So it can not be deleted.
YES you are right

I will edit my previous post .
Last edited on
Topic archived. No new replies allowed.