Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   What is the meaning of int in a function definition (http://www.velocityreviews.com/forums/t806516-what-is-the-meaning-of-int-in-a-function-definition.html)

fl 12-02-2011 09:15 AM

What is the meaning of int in a function definition
 
Hi,

When I search enum usage, I find the following example below. I find
that the definition:

inline Card::Suit operator++( Card::Suit &rs, int )

is interesting to me. What is the second parameter int for? Could you
tell me? Thanks.



..................................
// enumeration_declarations.cpp
class Card
{
public:
enum Suit
{
Diamonds,
Hearts,
Clubs,
Spades
};
// Declare two constructors: a default constructor,
// and a constructor that sets the cardinal and
// suit value of the new card.
Card();
Card( int CardInit, Suit SuitInit );

// Get and Set functions.
int GetCardinal(); // Get cardinal value of card.
int SetCardinal(); // Set cardinal value of card.
Suit GetSuit(); // Get suit of card.
void SetSuit(Suit new_suit); // Set suit of card.
char *NameOf(); // Get string representation of card.
private:
Suit suit;
int cardinalValue;
};

// Define a postfix increment operator for Suit.
inline Card::Suit operator++( Card::Suit &rs, int )
{
return rs = (Card::Suit)(rs + 1);
}

int main()
{
}

Nick Keighley 12-02-2011 10:09 AM

Re: What is the meaning of int in a function definition
 
On Dec 2, 9:15*am, fl <rxjw...@gmail.com> wrote:

> When I search enum usage, I find the following example below. > I find that the definition:
>
> inline Card::Suit operator++( Card::Suit &rs, int )
> is interesting to me. What is the second parameter int for?


repost this on comp.lang.c++

(or lookup pre- and post- increment)

don't bother to post the rest of the code

<snip>

Malcolm McLean 12-02-2011 01:46 PM

Re: What is the meaning of int in a function definition
 
On Dec 2, 11:15*am, fl <rxjw...@gmail.com> wrote:
> Hi,
>
> When I search enum usage, I find the following example below. I find
> that the definition:
>
> inline Card::Suit operator++( Card::Suit &rs, int )
>
> is interesting to me. What is the second parameter int for? Could you
> tell me? Thanks.
>

In C, there are two ++ operators, the pre-increment ++i, and the post-
increment i++.

The post increment form is more useful, because it returns the value
of i before you increment it. That's usually what you want.

i = 0;
x = array[i++];

will set x to array[0], and increment i to 1.

i = 0;
x = array[++i];

will increment i, then set x to array[1]. Occasionally you want this
second behaviour.

Anyway, in a language which is based on C, and allows operator
overloading, somehow you've got to distinguish between the pre-
increment and post-increment. The int is just a way of indicating
this, it's a special patch to the C++ compiler, or what is known as a
"hack".








All times are GMT. The time now is 05:37 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.