#include<iostream> using namespace std; class TestClass { public: TestClass() { str = nullptr; } TestClass(char*str) { int length = strlen(str); this->str = new char[length + 1]; /* Why increase length + 1 here if everything works without it!? */ for(int i = 0; i < length; i++) { this->str[i] = str[i]; cout << str[i] << endl; } this->str[length] = '\0'; /* Why this line of code if everything works fine without it */ } ~TestClass() { delete[] this->str; } private: char*str; }; int main() { TestClass a("Hello"); }
this->str
TestClass
strlen
'\0'
str
C
C++ std::string
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)