c++ 11 Copy constructor.

So, I've never used a copy constructor before and saw the following post:
http://www.cplusplus.com/forum/beginner/93795/

So I thought I'd have a go at it. I'm probably now where near it, but what ever.

Anyway, as far as I know, this was unable to happen before:
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
// copy constructor

#include <iostream>
#include <string>

class people
{
	public:
		people( std::string n ) : name( n ) {}
		people( std::string n, const people &p );
		~people() {}

		void setAge( int a )			{ age = a; }
		void setGender( char g )		{ gender = g; }
		void setName( std::string n )	{ name = n; }

		int getAge()					{ return age; }
		char getGender()				{ return gender; }
		std::string getName()			{ return name; }

	private:
		std::string name;
		char gender;
		int age;
};

people::people( std::string n, const people &p ) : name( n ), age( p.age ), gender( p.gender ) {}

void outputPeople( people &p )
{
	std::cout << "Name: " << p.getName() << '\n'
			  << "Age: " << p.getAge() << '\n'
			  << "Gender: " << p.getGender() << "\n\n";
}

void copyConstructor()
{
	people person_1( "Dave" );
	person_1.setAge( 28 );
	person_1.setGender( 'M' );

	people person_2( "Steve", person_1 );
	people person_3 = person_2;

	person_3.setName( "Pete" );

	outputPeople( person_1 );
	outputPeople( person_2 );
	outputPeople( person_3 );
}


I'm on about line 43. Has it always been possible to copy a class like this? Or is it new to C++11. I'm sure I've tried to copy a class before and ended up coding an overloaded operator, or used a function.

Thanks for any info! (:
It's always been possible. Using = when declaring the type/name like that on line 43 will call the copy constructor and not operator=().
Oh, ok! Thanks.
Topic archived. No new replies allowed.