What is the meaning of int in a function definition

F

fl

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()
{
}
 
N

Nick Keighley

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>
 
M

Malcolm McLean

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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top