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;
};
Printable View
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;
};
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.
Consider replacing NULL with nullptr as well, if your compiler supports it.
If the struct is in the header file, does this mean all this is inline?
Yes, but you can separate the implementation from the definition just as with classes.