how to exit from a member fuction

Hey all, without pasting all my code here, I'd like to know how to exit a class and return to main. It is a void member function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class MyClass
{
  int x, y;
public:
  void myProblem(int x, int y);
};

void MyClass::myProblem(int x, int y)
{
  if( x == y)
  {
     std::cout << "You win!"
     //what do i put here to exit the class and go straight to the main
     //instead of moving on through the program?
  }
else
{
  //some other code
}
}
Last edited on
Couple of points here.

First off, that's not a void member function, it's a constructor for your MyClass class and is called implicitly whenever a MyClass object is creative.

Second, the x and y values will always be uninitialised here, so that if statement isn't going to sanely check anything.

Finally, if you had a member function and called it then once the function exits scope it you'll return to the point from where it was called.

http://en.wikipedia.org/wiki/Call_stack
i edited it... What Im trying to do is play tic tac toe, and if the user gets 3 in a row, then exit the program (which is tested inside a void member function), if not, keep playing the game
I'd probably just have some boolean returning function for something like that.

1
2
3
4
bool HasWon( /*args*/ ) 
{
   return( /*winning conditions */);
}


Which could maybe be used in a loop to control flow:

1
2
3
4
while (!HasWon( /*args*/ )
{
   // Code to keep playing
}
Last edited on
doesnt work. if i do that, it just gets out of the member function, but still moves on through the program... it feels like i need to step out of each function and test if the return is true in each case to get out of it...

EDIT:
Got it to work thanks! Just needed to return it in the preceding function that called it
Last edited on
Topic archived. No new replies allowed.