Archived

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

Function not returning right?

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