Archived

This topic is now archived and is closed to further replies.

Xtreme

Copy Constructors() -> Confusing!!

Recommended Posts

hi everyone I want to point out that using copy constructors without knowing is very confusing!! For example, I wrote the following code to try it out:
#include <iostream>
using namespace std;

#include <cstring>

class Foo
{
	int x;
public:
	Foo() : x(0) { cout << "Foo() constructor called" << endl; } //constructor 1

	Foo(const int in) : x(in) { cout << "Foo(in) constructor called" << endl; } //constructor 2

	~Foo() { cout << "Foo() destructor called" << endl; } //destructor


	void Results()
	{
		cout << "X: " << x << endl;
	}
		
};

int main(int argc, char* argv[])
{
	Foo f1(1); //1 arg constructor called.

	f1.Results();
	Foo f2(f1); //Copy constructor called.

	f2.Results();
	Foo f3 = f2; //Same as Foo f3(f2);

	f3.Results();
	Foo f4(4);
	f4.Results();
	f3 = f4;
	f3.Results();
	return 0;
}

As you can see, f2 & f3 contain exactly the same values as f1 object. And then later f3 is a duplicate of f4. However, if I have the following code (introducing the definition of copy constructor (& operator=) f2 & f3 are no longer copies of f1. How come??

#include <iostream>
using namespace std;

#include <cstring>

class Foo
{
	int x;
public:
	Foo() : x(0) { cout << "Foo() constructor called" << endl; } //constructor 1

	Foo(const int in) : x(in) { cout << "Foo(in) constructor called" << endl; } //constructor 2

	
	Foo(const Foo &f) //Copy constructor

	{ 
		cout << "Copy constructor called" << endl; 
	}
	
	~Foo() { cout << "Foo() destructor called" << endl; } //destructor


	void Results()
	{
		cout << "X: " << x << endl;
	}
	
	
	void operator=(Foo &f)
	{
		cout << "hello" << endl;
	}
	
	
};

int main(int argc, char* argv[])
{
	Foo f1(1); //1 arg constructor called.

	f1.Results();
	Foo f2(f1); //Copy constructor called.

	f2.Results();
	Foo f3 = f2; //Same as Foo f3(f2);

	f3.Results();
	Foo f4(4);
	f4.Results();
	f3 = f4; // Operator= called.

	f3.Results();
	return 0;
}

Worse is the fact that f3 is no longer member-wise copied, but Operator function is called instead! This is wierd...

Share this post


Link to post
Share on other sites
this happens because in your copyconstructor and operator= functions you dont copy anything across, you just do cout statements. Add code to copy the data across and you''ll see it works fine and there is nuffin ''weird'' about it, its just doing what you told it to do

Share this post


Link to post
Share on other sites
Let me be more precise as what i call "wierd".

When you create an object, like say, f5, (with no arguments) it will "implicitly" call the default constructor even if it didnt have one.

Eg. Foo f5; //calls the default constructor right?

So, since it doesnt do anything i want to display it, so i include the definition for a default constructor.

Now, when you call f3(f2); //this calls the 'copy' constructor.

Again this is called implicitly right?

BUT! When I include the definition (like for the default constructor), the behaviour is different now.

This is what I mean.



[edited by - Xtreme on October 15, 2003 7:44:24 AM]

Share this post


Link to post
Share on other sites
The compiler generates copy ctors and op= that do member-wise copy only if you don''t define them. Since you defined them and since the ones you defined don''t do any copying, no more member-wise copying happens.

--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com

Share this post


Link to post
Share on other sites
When the compiler generates a copy constructor, it generates one that is equivalent to,


Foo(const Foo& o) : x(o.x) {}


Thus if you write a copy constructor that is different to this one, the behavior will change.

Also note that one-argument constructors that are not copy constructors, such as,


Foo(const int in) : x(in) { cout << "Foo(in) constructor called" << endl; } //constructor 2


Should generally be marked as explicit, to disallow implicit (and likely accidental) conversions between type int and Foo.

-Sirp.

Battle For Wesnoth: A Free fantasy turn-based strategy game - http://www.wesnoth.org

[edited by - Sirp on October 15, 2003 8:21:15 AM]

[edited by - Sirp on October 15, 2003 8:21:36 AM]

Share this post


Link to post
Share on other sites