c++ initiliazation what do you call it
#3 Members - Reputation: 113
Posted 10 August 2012 - 06:50 AM
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
#4 Moderators - Reputation: 5036
Posted 10 August 2012 - 07:02 AM
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.
#5 Members - Reputation: 113
Posted 10 August 2012 - 07:17 AM
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.
Edited by jtw, 10 August 2012 - 07:24 AM.
#6 Members - Reputation: 255
Posted 10 August 2012 - 07:36 AM
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;
#7 Members - Reputation: 113
Posted 10 August 2012 - 08:00 AM
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?
Edited by jtw, 10 August 2012 - 08:01 AM.
#9 Members - Reputation: 369
Posted 10 August 2012 - 08:37 AM
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.
#10 Members - Reputation: 113
Posted 10 August 2012 - 09:33 AM
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.
#11 Members - Reputation: 207
Posted 10 August 2012 - 10:30 AM
is a good example for one importat issue to remember: the * doesnt jump over to the next variable
so if u do
int *a, b, *c
you create an int pointer a, a regular int b and an another int pointer c
can be quite confusing.
Edited by FlyingDutchman, 10 August 2012 - 10:31 AM.
I open sourced my C++/iOS OpenGL 2D RPG engine :-)
See my blog: (Tutorials and GameDev)
#12 Members - Reputation: 2772
Posted 10 August 2012 - 12:56 PM
Yes, indeed. Professor Stroustrup prefers to convey only good practices and beneficial uses of the C++ programming language. In fact, he does discuss the multiple-declaration syntax in my copy of his book "The C++ Programming Language" and recommends against its use for all the same reasons documented here.there is probably a reason this syntax is not recognized in the bjarne stroustrop official reference book.
Professional Free Software Developer
#13 Members - Reputation: 1952
Posted 10 August 2012 - 11:26 PM
In Pascal one would do it as:
var a, b, c: integer;
My website dedicated to sorting algorithms
#14 Members - Reputation: 2408
Posted 11 August 2012 - 03:42 AM
I wanted to expose how the syntax does not hold up. Seems like it was tacked on as an afterthought.
Not really. Back in the days pre-C99 when you had to declare all your local variables at the top of a scope block before any statements, it was probably quite common to declare variables on one line. It is also fairly trivial for the parser to implement so is, like many features of C and C++, there if you want it, don't use if you don't.
Stroustrup often makes the point that the language does not dictate to the programmer the style in which s/he should code.






