c++ initiliazation what do you call it

Started by
12 comments, last by Aardvajk 11 years, 8 months ago
When you make a call like

UINT32 a = null, b;

What do you call that? I have noticed it in some code and I have never seen an initiliazation like that before. Visual C++ btw if it matters.
Advertisement
What's your question?

You just define two variables a and b, both being type UINT32 and a is initialized to null.
Hmm well I have never seen it done like that. Is there a name for it?

Another example

struct Some_Event** events = NULL, *event;

this is very strange syntax I have never learned about it. I wanted to know what it was called so i could look up documentation on its proper use. In my C++ Programming Language book by bjarne stroustrop i found nothing which is why i thought it was some kind of Microsoft visual C++ feature.

Thank you for any clarification
It is just declaring multiple variables on the same line. Some prefer the conciseness of this approach. Others believe that this is an error prone technique. For example:

int x, y = 0;

It is harder to see that x is uninitialised here than in the following:

int x;
int y = 0;


Also, for your Some_Event example however, where you are declaring effectively different types (different levels of indirection) is particularly confusing and probably best avoided.
I use the method you describe

Int a, b, c = null;

I found the examples in code on MSDN. I don't know why they would use such obscure syntax.

Am I the only person that has never seen it done like that?

Thank you for the reply's.
There is nothing obscure about this. In C/C++ you have always been able to declare more than one variable in one statement, separated with commas.

int a, b, c;

is the same as

int a;
int b;
int c;

However, as has already been said, your example doesn't initialize a and b. Only c is set to null.
You can (of course) also initialize to different values:

int a = 3, i = 0, n = 42;
I would of thought you would have to make it like

Int a = null, int b;

else how does the compiler's know what data type you mean. Sure it's easy to see that the first one was for int so the second one must mean int too. I expected some kind of rule to say you had to declare a data type.

I can do

Int a = null, float b = null, c;

Is c an int or a float?
Probably float, it's easy to test.
Try to initialize/assign c to 1.5f and compiler will give warning if it's not float.
The example above won't compile, as you need a ; to terminate the statement before the next type name.

i.e:

int a = NULL, float b = NULL, c; //error: type 'float' unexpected
int a = NULL; float b = NULL, c; //no error here, but bad form to write code like this generally.

You can also find more information here: http://www.cplusplus.com/doc/tutorial/variables/

I'd also like to note that NULL shouldn't really be used to initialize an int or a float, use 0 or 0.0f instead is clearer as to your intention.
Yes thank you I know I was just posing a question I already knew the answer to. I wanted to expose how the syntax does not hold up. Seems like it was tacked on as an afterthought.

Yes of course always use 0.0f for floats i was just making a quick example my apology.

thank you again for the reply. there is probably a reason this syntax is not recognized in the bjarne stroustrop official reference book.

This topic is closed to new replies.

Advertisement