Advertisement

extra return stuff...

Started by April 17, 2002 06:50 AM
4 comments, last by elis-cool 22 years, 7 months ago
I dont really know where this belongs so I''ll put it in here...
  
class AClass
{
      public:
      int GetSomething(){return var;}
      private:
      int var;
};
// This returns true if first num is greater than the other

bool Func(AClass Cclass, AClass CotherClass)
{
      return Cclass.GetSomething() > CotherClass.GetSomething();
}  
Now obviouly this is not a whole program but I saw something similer to is and was just wondering if this works(I presume it does) and if so why and how? I mean the member functions would be returning values and so I dont get it... CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
What''s not to get? (CClass.GetSomething() > CotherClass.GetSomething()) evaluates to a boolean, and that''s what func returns...

codeka.com - Just click it.
Advertisement
Yes, those methods return integers. And those numbers are then compared. The result of the compare is a boolean.
e.g:


  bool Func(X, Y){  return X.GetSomething() > Y.GetSomething();  // imagine X.GetSomething() is 1  // and Y.GetSomething() is 2  // then 1 > 2 false  // i.e the mothod returns false in this case}    

baumep
but I thought there would have had to been an if() expression for it to come out as true or false...
So I can use < > && || == <= => anywhere in my code and it will evaluate the statments on either side to a true or false?

CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
yes, you can use the return values from functions immediately like that

it''s the same as saying

if (Cclass.GetSomething() > CotherClass.GetSomething()
return true;
else
return false;

it saves typing and looks neater. in c/c++ anything that evaluates to 0 is false and anything non-zero is considered true...

that is why stuff like

while (1)
{
}

will be an infinate loop and :

while(0)
{
}

will not execute.

hope that helps
-Pac
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
quote: Original post by elis-cool
but I thought there would have had to been an if() expression for it to come out as true or false...

A selection statement takes an expression as its argument. That expression must evaluate to a bool type or something that is convertible to a bool type. if merely branches based on the resultant bool.
quote:
So I can use < > && || == <= => anywhere in my code and it will evaluate the statments on either side to a true or false?

It will evaluate the entire expression and return the result of that expression. If it is a conditional expression, the result will be of bool type.

[C++ FAQ Lite | ACCU | Boost | Learning C++]

[edited by - SabreMan on April 17, 2002 8:52:55 AM]

This topic is closed to new replies.

Advertisement