Delegating Constructor :
In Java, we can one constructor from another constructor, which is not possible in c++
class Cat
{
public:
Cat() { ... }
Cat(int a) { Cat(); doOtherThings(a); }
};
// But in case of C++ 03, we achieve this through creating a new function,
class Cat
{
init() { ... };
public:
Cat() { init(); }
Cat(int a) { init(); doOtherThings(); }
};
// With C++ 11, we can call constructor through an initializer list, also we can perform initialization for class data members.
class Cat {
int age = 9;
public:
Cat() { ... }
Cat(int a) : Cat() { doOtherThings(); }
};
Limitation:
Cat() has to be called first.
i.e. The constructor can't be called in middle or at the last in the other constructor.
In Java, we can one constructor from another constructor, which is not possible in c++
class Cat
{
public:
Cat() { ... }
Cat(int a) { Cat(); doOtherThings(a); }
};
// But in case of C++ 03, we achieve this through creating a new function,
class Cat
{
init() { ... };
public:
Cat() { init(); }
Cat(int a) { init(); doOtherThings(); }
};
// With C++ 11, we can call constructor through an initializer list, also we can perform initialization for class data members.
class Cat {
int age = 9;
public:
Cat() { ... }
Cat(int a) : Cat() { doOtherThings(); }
};
Limitation:
Cat() has to be called first.
i.e. The constructor can't be called in middle or at the last in the other constructor.
Comments
Post a Comment