I totally disappointed. I have class, there I have private structure. And what's the silly problem: I just can't preinitialize some variables!
What I mean:
I need:
struct someStruct
{
someStruct *next = NULL;
int number;
};
I just want to create easy dynamic list, adding new elements from heap.
And what I should do?
struct someStruct
{
someStruct *next;
int number;
};
Put
someStruct *newElement = new someStruct;
newElement.next = NULL;
every time? I can forget about this.
Please help me. Because it's not a problem when I need to add 1 useless string. But what if I have 50 default variables?
You cannot initialize class members when you declare them. Instead, you need a constructor :
struct someStruct {
someStruct(): next(NULL), number(0) {}
someStruct *next;
int number;
};
Alternatively, for POD (plain old data) , you could use your original class with no constructors and use someStruct *newElement = new someStruct();
to create a someStruct
. This initializes the members to zero.
Also, C++11 supports in-class member initializers .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.