I was just going through random pages on Cprogramming.com and noticed the Constructors and Destructors tutorial/example page. They have used the following method of defining a constructor:
class String
{
private:
char *str;
int size;
public:
String() : str(NULL), size(0) { } // <- This statement
String(int size) : str(NULL), size(size) { // <- And this one
str = new char[size];
}
}
I've been using the good old fashioned definition of constructors with the magic this
pointer:
String() {
this->str = NULL;
this->size = 0;
}
String(int size) {
this->size = size;
this->str = new char[size];
}
Is there any added benefit in the first declaration beside the obvious smaller code (less number of lines)?
PS: It has been quite a few years since I last did write something in C++.