Jump to content
  • Advertisement

Archived

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

elis-cool

extra return stuff...

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

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

Share this post


Link to post
Share on other sites
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

}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!