What is the meaning of int in a function definition

Discussion in 'C Programming' started by fl, Dec 2, 2011.

  1. fl

    fl Guest

    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()
    {
    }
     
    fl, Dec 2, 2011
    #1
    1. Advertising

  2. On Dec 2, 9:15 am, fl <> 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>
     
    Nick Keighley, Dec 2, 2011
    #2
    1. Advertising

  3. On Dec 2, 11:15 am, fl <> 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".
     
    Malcolm McLean, Dec 2, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,221
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,648
    Old Wolf
    Jan 20, 2004
  3. dean
    Replies:
    7
    Views:
    568
    Andrey Tarasevich
    Mar 30, 2008
  4. Replies:
    4
    Views:
    769
  5. Steve Hicks
    Replies:
    2
    Views:
    1,268
Loading...

Share This Page