473,472 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

comma operator

bb
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.
Nov 29 '07 #1
5 1347
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
Nov 29 '07 #2
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.comwrote:
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.
Nov 29 '07 #3
bb

Thanks guys for the explanation.
Nov 29 '07 #4
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>
Nov 29 '07 #5
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
Nov 29 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
5
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y,...
2
by: benben | last post by:
I am looking for a good example of overloading operator , (operator comma) Any suggestions? Ben
7
by: Koster | last post by:
Hi folks, As I understand it, amongst other things, the comma operator may be used to cause any number of expressions to be evaluated (who's results are thrown away except the last) where only...
11
by: Shawn Odekirk | last post by:
Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal...
4
by: G Patel | last post by:
Hi, I've read a book on C, and I understand how comma operators work, but my book didn't say that the comma operators between function arguments were not really comma operators (even though it...
2
by: grid | last post by:
Hi, I need some clarifications on how the comma operator is used to return values from function like macros.I saw a typical implementation as : #define sigfillset(ptr) ( *(ptr) &= ~(sigset_t)0 ,...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
1
by: CJK | last post by:
this is what i have so far: #include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; class Person {
15
by: Lighter | last post by:
In 5.3.3.4 of the standard, the standard provides that "The lvalue-to- rvalue(4.1), array-to-pointer(4.2),and function-to-pointer(4.3) standard conversions are not applied to the operand of...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.