Calling the constructor in Reset() instead

I was just wondering… Is it possible to call the constructor in the method Reset() instead, as the constructor would have the initialization values?

FBullCowGame::FBullCowGame()
{
	constexpr int WORD_LENGTH = 8;
	constexpr int MAX_TRIES = 8;
	MyWordLength = WORD_LENGTH;
	MyMaxTries = MAX_TRIES;
	MyCurrentTry = 1;
}

void FBullCowGame::Reset()
{
	FBullCowGame();
	return;
}

I think you can’t, because Reset() is a void function, and void functions can’t modify values unless they are called by reference which wasn’t done in this lecture.

1 Like

Technically you can call the constructor from another method as you are doing here, but in general it’s not common practice. The constructor of a class is meant for object instantiation. All you are doing in the Reset() method by calling the constructor is creating a temporary object and then immediately destroying it. A better solution would be to move the code in the constructor to an Init() method, which can be called from Reset(), and can also return a value (typically a bool or an Enumeration to indicate success or failure). Another common practice is using what’s known as an initializer list for members that should be initialized when a new object is instantiated (that might be getting ahead though, I haven’t gone through this section so I’m not sure if Ben covers them later on).

Side note - you don’t need the return; at the end of Reset() since it is a void method.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms