Hallo,
Tried searching, but couldnt find anything, so here it goes:
Is there a way to initialise a struct in a function like you can with a class?
Thanks,Code:struct point { float x,y, } someFunction(point(30,30)); <---Does not work
Hallo,
Tried searching, but couldnt find anything, so here it goes:
Is there a way to initialise a struct in a function like you can with a class?
Thanks,Code:struct point { float x,y, } someFunction(point(30,30)); <---Does not work
Yes. In C++, a struct is almost identical to a class, the only difference is that by default the members of a struct are public while the members of a class are private. You can write a constructor for your struct just like you would for the class.
abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "
[edit]Removed code that is entirely incorrect in the C++ forum, but would have been pretty cool in the C forum
I'm going to bed... way too late[/edit]
Last edited by Brad0407; 02-23-2008 at 11:20 PM.
Don't quote me on that... ...seriously
structs can certainly have constructors, destructors and member functions in C++ (they can not in C).
in fact, so long as you are careful to specify public/private appropriately, you could go through a C++ program and replace the word class with the word struct and the program would compile exactly the same.
Last edited by jEssYcAt; 02-23-2008 at 11:19 PM.
abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "
Brad that is not a correct statement for C++. In C++ a struct is exactly the same as a class except for struct is default to public scope, and class is default to private.