how do you intialize struct members?
how do you intialize struct members?
A struct can have constructors and functions associated with it just like a class can. Just make a constructor for the struct that assigns default values to the struct's data members. Is this what you mean or are you talking about something else?
Should output: 5 9.9Code:#include <iostream> struct mystruct { int iVal; float fVal; mystruct() : iVal(5), fVal(9.9f) {} }; int main() { mystruct item; std::cout << item.iVal << ' ' << item.fVal << std::endl; return 0; }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
>how do you intialize struct members?
The alternative is an initialization list upon variable creation:
Code:#include <iostream> struct mystruct { int iVal; float fVal; }; int main() { mystruct item = { 5, 9.9 }; std::cout<< item.iVal <<' '<< item.fVal <<std::endl; }
My best code is written with the delete key.