Jump to content
  • Advertisement

Archived

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

Luna

Why?

This topic is 5219 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
Advertisement
You should post what it displays on GCC. Without that information, there''s little to be said (no, I won''t compile this code).

Oh, and don''t use <iostream.h>; use <iostream>.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!