Archived

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

Why?

This topic is 5002 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a small question. I have this code:
#include <iostream.h>

class X
{
public:
	int a;
	X()
	{
		cout<<"implic\n";
	}
	X(const X&altu)
	{
		//a=altu;

		cout<<"copy\n";
	}
	~X()
	{
		cout<<"dest\n";
	}
};

X f(X a)
{
	X t=a;
	return t;
}

int main()
{
	X x,y;
	y=f(x);
	return 1;
}
There is nothing wrong with it. But on VC6 it displays 2const impl,3 const copy, 5 dest, and on gcc it displays...smth else (i forgot) Why is this difference? [edited by - Oluseyi on April 6, 2004 4:47:12 AM]

Share this post


Link to post
Share on other sites
gcc may be employing a return value optimization. Essentially, the object t in function f is being allocated in the stack frame of the calling function, rather than in f''s own stack frame. This avoids calling the copy constructor when t is returned at the end of the function.

Share this post


Link to post
Share on other sites