What does ... mean

This is a discussion on What does ... mean within the C++ Programming forums, part of the General Programming Boards category; int decr(int a) { a--; if(a<0) a=2; return a; } int incr(int a) { a = (a+1)%3; return a; } ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    34

    What does ... mean

    int decr(int a)
    {
    a--;
    if(a<0)
    a=2;
    return a;
    }

    int incr(int a)
    {
    a = (a+1)%3;
    return a;
    }



    int main(void)
    {
    int n;
    oper incr_decr;







    ok there is a snippet of code. My question is how does
    oper incr_decr work and what happens in

    incr_decr = n%2 ? decr : incr;

    if you need more of the code i can supply it.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    I have no clue about the first code (think I need more code)



    When you see the c? a:b operator this is what happens:

    If c is true, a is returned. If c is false, b is returned. That is what I htink at least.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    #include<iostream.h>


    class cell {
    int disc;
    cell *next;
    cell (int d)
    {
    disc = d;
    next = this;
    }
    cell (int d, cell *n)
    {
    disc = d;
    next = n;
    }
    friend class list;
    };

    class list{
    cell *rear;
    public:
    int empty() { return rear == rear->next; }


    void pop(int &i)
    {
    i = 0;
    if( empty() ) return;
    cell *front = rear->next;
    rear->next = front->next;
    i = front->disc;
    delete front;
    return;
    }

    void push(int d)
    {
    rear->next = new cell(d,rear->next);
    }



    list() { rear = new cell(0); }

    ~list()
    {
    int i,j,l;
    long k;
    while(!empty()) pop(i);
    }
    };


    list pegs[3];

    typedef int (*oper)(int);

    int decr(int a)
    {
    a--;
    if(a<0)
    a=2;
    return a;
    }

    int incr(int a)
    {
    a = (a+1)%3;
    return a;
    }



    int main(void)
    {
    int n;
    oper incr_decr;
    int curr=0;
    int p1 = 1, p2 = 2;
    int d0,d1,d2;
    cout << "Enter Number Of Discs:";
    cin >> n;
    for(int i = n; i > 0; i--)
    pegs[0].push(i);
    incr_decr = n%2 ? decr : incr; //conditional statement
    while(1)
    {
    //rest of code

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    OK, first, look at this line:

    oper incr_decr;

    This declares a variable called incr_decr to be of type "oper". This is not a native type, so it's user defined. We see this is defined here:

    typedef int (*oper)(int);

    so a variable of type "oper" is a pointer to a function which takes an integer and returns an integer.

    What it is doing is determining, by the statement:

    n%2

    if it should increment or decrement. It assigns incr_decr to point to either incr() or decr(), and presumably it will call the function (using the pointer) later on.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21