Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   comma operator (http://www.velocityreviews.com/forums/t556021-comma-operator.html)

bb 11-29-2007 03:09 PM

comma operator
 
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.

Victor Bazarov 11-29-2007 03:15 PM

Re: comma operator
 
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



Supat Wongwirawat 11-29-2007 03:37 PM

Re: comma operator
 
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 <muralibal...@gmail.com> 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.



bb 11-29-2007 06:21 PM

Re: comma operator
 

Thanks guys for the explanation.

Default User 11-29-2007 07:23 PM

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>

Tomás Ó hÉilidhe 11-29-2007 10:23 PM

Re: comma operator
 
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


All times are GMT. The time now is 07:58 PM.

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