comma operator

Discussion in 'C++' started by bb, Nov 29, 2007.

  1. bb

    bb Guest

    Hi,

    Please could someone throw some light on why the following definitions
    are not the same?

    class A {

    const int& a, b; // b does not get defined as a
    ref. but a plain int

    const int& x; const int& y; // obviously, both are refs.

    ...

    };

    Thanks.
     
    bb, Nov 29, 2007
    #1
    1. Advertising

  2. bb wrote:
    > Please could someone throw some light on why the following definitions
    > are not the same?
    >
    > class A {
    >
    > const int& a, b; // b does not get defined as a
    > ref. but a plain int
    >
    > const int& x; const int& y; // obviously, both are refs.
    >
    > ...
    >
    > };


    It really has nothing to do with comma operator, you know.

    The rules of declarations are such that '*' and '&' are part of the
    individual object declaration, whereas the type (the 'const int' part
    in your example) is common for all objects. IOW, the same thing would
    happen if you declared 'a' as a pointer to const int:

    const int* a, b; // 'a' is a pointer, 'b' is not.

    To avoid mistakes like that, declare each object in its own separate
    statement, or use typedefs:

    typedef const int *pointerToConstInt;

    pointerToConstInt a, b; // both 'a' and 'b' are pointers now

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 29, 2007
    #2
    1. Advertising

  3. It is the C++ syntax.
    For reference and pointer you cannot declare like this

    T& a,b,c;
    T* a,b,c;

    Only a will be reference/ pointer.
    You have to specify * or & for every variable in the declaration.

    const int &a, &b; will do what you want to accomplish.

    Hope that helps.

    On Nov 29, 10:09 am, bb <> wrote:
    > Hi,
    >
    > Please could someone throw some light on why the following definitions
    > are not the same?
    >
    > class A {
    >
    > const int& a, b; // b does not get defined as a
    > ref. but a plain int
    >
    > const int& x; const int& y; // obviously, both are refs.
    >
    > ...
    >
    > };
    >
    > Thanks.
     
    Supat Wongwirawat, Nov 29, 2007
    #3
  4. bb

    bb Guest

    Thanks guys for the explanation.
     
    bb, Nov 29, 2007
    #4
  5. bb

    Default User Guest

    Re: comma operator - TPA

    Supat Wongwirawat wrote:

    > It is the C++ syntax.


    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or the group FAQ list:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html>
     
    Default User, Nov 29, 2007
    #5
  6. bb:

    > const int& a, b; // b does not get defined as a
    > ref. but a plain int
    >
    > const int& x; const int& y; // obviously, both are refs.



    In the "grammar" of C++, the ampersand and the asterisk are part of the
    object's name rather than its type. That's why I write: int *p instead of
    int* p.

    Write a type, then write the names after it, separated by commas:

    int *a, *b, *c, *d;

    This grammar is what leads to declarations such as:

    int (*p(double))[5];

    Also don't confuse the comma within structs with the _actual_ comma
    operator.

    --
    Tomás Ó hÉilidhe
     
    Tomás Ó hÉilidhe, Nov 29, 2007
    #6
    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. Paul Davis
    Replies:
    7
    Views:
    622
    Paul Davis
    Jul 17, 2003
  2. Derek
    Replies:
    6
    Views:
    384
    Old Wolf
    Apr 13, 2004
  3. Koster

    Yet another comma operator question

    Koster, Jan 16, 2004, in forum: C Programming
    Replies:
    7
    Views:
    387
    Old Wolf
    Jan 18, 2004
  4. Michael Scarlett

    the whys of the comma operator

    Michael Scarlett, Jun 10, 2004, in forum: C Programming
    Replies:
    8
    Views:
    328
    Michael Scarlett
    Jun 19, 2004
  5. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    508
    Barry Schwarz
    Feb 8, 2005
Loading...

Share This Page