Is there a way to combine these constructors into one?
Code:struct Node { Node() : next(NULL) { } Node(string data) : next(NULL) : this->data(data) { } string data; Node *next; };
Is there a way to combine these constructors into one?
Code:struct Node { Node() : next(NULL) { } Node(string data) : next(NULL) : this->data(data) { } string data; Node *next; };
I don't think so. Is there a reason why you would want to?
Well, you could write:
You might want to consider declaring the constructor as explicit.Code:struct Node { Node(const std::string& data_ = std::string()) : data(data_), next(NULL) {} std::string data; Node* next; };
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It's a default argument, when you supply fewer arguments to the function than it expects, the default value is used to initialize the argument.
"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
If the struct is in the header file, does this mean all this is inline?