By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,137 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

What is the difference??

P: n/a
Hi all,

What is the difference in both types of intializations?.

Class Abc {

int x,y,z;

public:
Abc() { x=0, y=0, z=0; }
};
Class Abc {
int x,y,z;

public:
Abc():x(0),y(0),z(0) { }
};
Is there any difference in these 2 kinds of intializations?. Which one
could be better to use?.

Thanking you.

Dec 26 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ba********@gmail.com wrote:
Hi all,

What is the difference in both types of intializations?.

Class Abc {

int x,y,z;

public:
Abc() { x=0, y=0, z=0; }
};
Class Abc {
int x,y,z;

public:
Abc():x(0),y(0),z(0) { }
};
Is there any difference in these 2 kinds of intializations?. Which one
could be better to use?.
The first one is not initialization, it's assignment.

The answer to your question is in the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-10.6

HTH,
- J.
Dec 26 '06 #2

P: n/a

ba********@gmail.com wrote:
Hi all,

What is the difference in both types of intializations?.

Class Abc {

int x,y,z;

public:
Abc() { x=0, y=0, z=0; }
Thats a syntax error:
Abc() { x=0; y=0; z=0; }
};
Class Abc {
int x,y,z;

public:
Abc():x(0),y(0),z(0) { }
};
Is there any difference in these 2 kinds of intializations?. Which one
could be better to use?.
The first would allocate and then assign, the second allocates +
initializes simultaneously.

Dec 26 '06 #3

P: n/a
Salt_Peter wrote:
ba********@gmail.com wrote:
Class Abc {

int x,y,z;

public:
Abc() { x=0, y=0, z=0; }

Thats a syntax error:
Abc() { x=0; y=0; z=0; }
You sure about that?

Dec 26 '06 #4

P: n/a

Squeamizh wrote:
Salt_Peter wrote:
ba********@gmail.com wrote:
Class Abc {
>
int x,y,z;
>
public:
Abc() { x=0, y=0, z=0; }
Thats a syntax error:
Abc() { x=0; y=0; z=0; }

You sure about that?
no such thing as Class.

Dec 26 '06 #5

P: n/a

"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
>
ba********@gmail.com wrote:
>Hi all,

What is the difference in both types of intializations?.

Class Abc {

int x,y,z;

public:
Abc() { x=0, y=0, z=0; }

Thats a syntax error:
No, it's not. It actually achieves the same effect as:
Abc() { x=0; y=0; z=0; }
Look up 'comma operator'.

-Mike
Dec 27 '06 #6

P: n/a

Mike Wahler wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...

ba********@gmail.com wrote:
Hi all,

What is the difference in both types of intializations?.

Class Abc {

int x,y,z;

public:
Abc() { x=0, y=0, z=0; }
Thats a syntax error:

No, it's not. It actually achieves the same effect as:
Abc() { x=0; y=0; z=0; }

Look up 'comma operator'.
thanks, i stand corrected.

Dec 27 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.