Skip to main content
gamedev.net
🔒 Locked

Function not returning right?

Started by Drakon Mar 29, 2004 at 5:18 PM 0 replies 250+ views
Original Post
Drakon
Drakon
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.
"Go for the eyes, Boo, go for the eyes!"
SiCrane
SiCrane
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()

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.