मैं बस Cprogramming.com पर यादृच्छिक पृष्ठों के माध्यम से जा रहा था और Constructors and Destructors ट्यूटोरियल/उदाहरण पृष्ठ पर ध्यान दिया। वे एक निर्माता को परिभाषित करने के निम्नलिखित विधि का इस्तेमाल किया है:इस कन्स्ट्रक्टर परिभाषा का लाभ क्या है
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];
}
}
मैं जादू this
सूचक के साथ कंस्ट्रक्टर के अच्छे पुराने जमाने परिभाषा का उपयोग किया गया है:
String() {
this->str = NULL;
this->size = 0;
}
String(int size) {
this->size = size;
this->str = new char[size];
}
कोई अतिरिक्त लाभ है स्पष्ट छोटे कोड (लाइनों की कम संख्या) के बगल में पहली घोषणा में?
पीएस: यह पिछले कुछ वर्षों से रहा है क्योंकि मैंने पिछली बार सी ++ में कुछ लिखा था।
http://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-list –