Problem with returning objects

Here's my code (console application):

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 <iostream.h>

class Me
{
private:
	int age;

public:
	Me (Me & rhs)
	{
		cout << "Copy Constructor...\n";
	}

	Me()
	{
		cout << "Constructor...\n";
	}

	int GetAge ()
	{
		return age;
	}

	~Me()
	{
		cout << "Destructor...\n";
	}

	void SetAge (int MyAge)
	{
		age = MyAge;
	}
};

Me CreateDummy();    // if i make this function return a reference then    
Me CreateDummy ()   //the output is correct. But i just want to return by value
{
	Me * parunlfc = new Me;
	parunlfc -> SetAge(21);
	return *parunlfc;
}

int main ()
{
	Me Arun;
	Arun.SetAge(18);
	Me Twin;          
	Twin = CreateDummy(); //i guess problem is here
	cout << "twin : " << Twin.GetAge() << "\n";
	cout << "Arun : " << Arun.GetAge() << "\n";
	return 0;
}



I thought i had understood returning objects by value and reference, but it just wouldn't compile!!!
What is the problem with the above code...please explain what is happening, not just solution...

Thanks,
Arun ;)
Last edited on
You have three problems.

First, a copy constructor has the signature

1
2
3
4
class my_class {
  public:
    my_class( const my_class& );
};


Note the const. Your declaration does not have "const" so it is not a copy constructor.

Having said that, you don't need a copy constructor at all, since the default one provided by the compiler will suffice. [Note that if your constructor actually were a copy constructor, it is not actually copying the source object; ie, it does not assign age.]

Last, CreateDummy() also creates a sort-of memory leak since it dynamically allocates a new Me object but then returns it by value rather than by pointer. It does not need to use new.

I hope this is what you're after, it compiles for me (see beneath the code for explanation):

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

using namespace std; // modified

class Me
{
private:
	int age;

public:
	Me (const Me &) // modified
	{
		cout << "Copy Constructor...\n";
	}

	Me()
	{
		cout << "Constructor...\n";
	}

	int GetAge ()
	{
		return age;
	}

	~Me()
	{
		cout << "Destructor...\n";
	}

	void SetAge (int MyAge)
	{
		age = MyAge;
	}
};

Me CreateDummy();
Me CreateDummy ()
{
	Me parunlfc; // modified
	parunlfc.SetAge(21); // modified
	return parunlfc; // modified
}

int main ()
{
	Me Arun;
	Arun.SetAge(18);
	Me Twin;
	Twin = CreateDummy();
	cout << "twin : " << Twin.GetAge() << "\n";
	cout << "Arun : " << Arun.GetAge() << "\n";
	cin.get(); // modified
	return 0;
}


Okay:

modified 1: changed iostream.h to iostream - basically .h is deprecated...
modified 2: so that cout calls std::cout
modified 3: changed Me & to const Me & - as far as I was aware the copy constructor needed the const keyword
modified 4,5,6: when I used this code I couldn't use GetAge to return 21, so it got changed to stop using the pointers
modified 7: just to see the output
Get rid of the copy constructor. It's a waste of space. You only need it if your actually going to over-ride it's default functionality.

To jsmith...
Thanks for your help so far...
Just let me know if i am right on the following points ( to make sure i did not misunderstand):

1. The job of the default copy constructor is to make an exact copy of an object by assigning all its member variables.
2. The object that the function CreateDummy() returns to main, has been created by the copy constructor that i defined. The object is there in memory, just that it's member 'age' has not been assigned any value as i expected.
3. If i declare and define my own copy constructor, i have to explicitly assign member variables of the new object to the source object's variables.

But if i were to keep the copy constructor in the code above, how would i do to assign the value to age. I mean i don't know the name of the object that the copy constructor is creating!

1. Yes
2. Yes
3. Yes, But you only need to do your own if you need a deep-copy. In this instance, the default Shallow-copy by the default copy constructor is sufficient. So get rid of the copy constructor.

1
2
3
Me (const Me &me)  {
 age = me.age;
}
Last edited on
Thanks buddies...
jsmith and zaita...
You were really helpful!
Copy constructor:
1
2
3
4
5
Me( const Me & me )
{
    cout << "Copy Constructor...\n";
    age = me.age;
}


Note that this copy constructor does override the default copy constructor to provide more functionality (it prints the debug message).
Topic archived. No new replies allowed.