Jump to content
  • Advertisement

Archived

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

Drakon

Function not returning right?

This topic is 5319 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

The problem here is that I get a prompt returning 0 0 Press any key to continue. instead of 0 333 Press blah blah
class TestClass
{
public:
	int	TestNum;
};

vector<TestClass> TC(5);

TestClass TestFunction()
{
	return TC[0];
}

void main()
{
	TC[0].TestNum = 0;
	cout << TC[0].TestNum << endl;
	TestFunction().TestNum = 333;
	cout << TC[0].TestNum << endl;
}
Though, if I replace TestFunction.TestNum = 333; with TC[0].TestNum = 333;, it works. Though, I dont want to use that... or else, I wouldn''t be testing this.

Share this post


Link to post
Share on other sites
Advertisement
You''re function is returning a copy of the object, which isn''t what you want. Change the function signature of TestFunction() to return by reference. i.e.:

TestClass & TestFunction()

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!