Constructor delegation from body of constructor | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Constructor delegation from body of constructor

Not sure why but seems some issue to save the new code from playground on windows 10, so pasting code into query itself: class myString { public: myString() : m_pCharName(nullptr), m_intSize(0) { cout << "null contructor called\n"; } ~myString() { cout << "Destructor called\n"; if (m_pCharName) delete[] m_pCharName; } myString(const char* pInput) { if (pInput == nullptr) myString(); else { m_intSize = strlen(pInput); m_pCharName = new char[m_intSize + 1]; strcpy_s(m_pCharName, m_intSize + 1, pInput); } } private: char* m_pCharName; int m_intSize; }; Question is on constructor delegation. Is below line proper? if (pInput == nullptr) myString(); I want to call non parametric constructor in case of input as nullptr.

3rd May 2022, 11:22 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
0
Let me write in different words : Why code below observes memory issue ? https://code.sololearn.com/cYBe2NnYd4Ek/?ref=app
7th May 2022, 9:22 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Hi community , Any solution on this , plz ?
30th Sep 2022, 7:15 AM
Ketan Lalcheta
Ketan Lalcheta - avatar