Copy consstructor and the explicit keyword

Hello, im trying to understand copy constructor but I can not get a point. Can anyone give me a simple example why and when should I use copy constructor?

The second thing is the keyword explicit if anyone can explain me what does it mean?
I already know that explicit keyword disable implicit conversion and that if constructor is explicit
I can not initialize like this:
Classname obj=10;
But i do not understand if it disable implicit conversion than i should got an error if i write
Classname obj=10.5;
and parameter of explicit constructor is int a but i checked i can do that and works perfectly fine, why than is explicit keyword?

Thanks for answers!
But i do not understand if it disable implicit conversion than i should got an error if i write
Classname obj=10.5;
and parameter of explicit constructor is int a but i checked i can do that and works perfectly fine, why than is explicit keyword?


Really?? Which compiler are you using then??
guestgulkan sorry my mistake
I can write this
1
2
3
4
5
6
7
8
9
10
11
class F{
    public:
       explicit F(int i):x(i){}
       int x;
};
int main()
{
    F obj (10.3);
    cout << obj.x << endl;
    return 0;
}


But still explicit keyword does not disable implicit conversion because x is 10 in above case.
Here's a link.
http://en.wikipedia.org/wiki/Object_copy

Now I'll let the code do the explaining, mostly.
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const int defaultSize = 10; // default size of array

class ShallowCopy {
public:

	int *numbers; // dynamic array
	int size; // size of array

	// default constructor
	ShallowCopy():
		numbers(new int[defaultSize]),
		size(defaultSize)
	{
		for (int i=0; i < defaultSize; ++i)
			numbers[i] = i;
	}

	// non-explicit constructor from int, uh-oh
	ShallowCopy(int size):
		numbers(new int[size]),
		size(size)
	{
		for (int i=0; i < size; ++i)
			numbers[i] = i;
	}

	// we don't define a copy constructor, uh-oh

	// destructor to prevent memory leaks
	~ShallowCopy()
	{
		delete[] numbers;
	}
};

#include <iostream>

void printShallowCopy(const ShallowCopy &sc)
{
	for (int i=0; i < sc.size; ++i)
		std::cout << sc.numbers[i] << ' ';

	std::cout << std::endl;
}

int main()
{
	printShallowCopy(6);
	// problem: ShallowCopy(int) not "explicit"

	ShallowCopy sc1; // defaultSize == 10
	ShallowCopy sc2(5);

	printShallowCopy(sc1);
	printShallowCopy(sc2);

	// sc1 = sc2; // doesn't work, no operator= defined

	ShallowCopy sc3(sc2);
	// problem: no "deep" copy constructor

	sc2.numbers[2] = 777;
	sc2.numbers[3] = 777;
	sc2.numbers[4] = 777;	

	printShallowCopy(sc3); // why did the data in sc3 change?

	std::cout << sc2.numbers << '\t' << sc3.numbers << std::endl;
	// because it's the same pointer used in both sc2 and sc3!
}


0 1 2 3 4 5 
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 
0 1 777 777 777 
0x3e2470	0x3e2470

Catfish2 thanks it helps now I really understand copy constructor
Last edited on
Topic archived. No new replies allowed.