??????C++98????????????????????????????????????????????????????????????????????????????д???ó?????C++11?ж?????????????????????????????????????C++11??????????????????????????????????????????C++98?е??????????????????????????????????????????????????????
????????????????????????????????????C++????????????C++11???????????????C++????????????????????????????????????????????????ú???????á?
???????????????
????????????C++98?е??????????????????????
class MyString {
public:
MyString() {
_data = nullptr;
_len = 0;
printf("Constructor is called! ");
}
MyString(const char* p) {
_len = strlen (p);
_init_data(p);
cout << "Constructor is called! this->_data: " << (long)_data << endl;
}
MyString(const MyString& str) {
_len = str._len;
_init_data(str._data);
cout << "Copy Constructor is called! src: " << (long)str._data << " dst: " << (long)_data << endl;
}
~MyString() {
if (_data)
{
cout << "DeConstructor is called! this->_data: " << (long)_data << endl;
free(_data);
}
else
{
std::cout << "DeConstructor is called!" << std::endl;
}
}
MyString& operator=(const MyString& str) {
if (this != &str) {
_len = str._len;
_init_data(str._data);
}
cout << "Copy Assignment is called! src: " << (long)str._data << " dst" << (long)_data << endl;
return *this;
}
operator const char *() const {
return _data;
}
private:
char *_data;
size_t   _len;
void _init_data(const char *s) {
_data = new char[_len+1];
memcpy(_data?? s?? _len);
_data[_len] = '';
}
};
MyString foo()
{
MyString middle("123");
return middle;
}
int main() {
MyString a = foo();
return 1;
}
???????????????????н????????????????????????????????????????????????????????????????????????????????????????C++???????????????????????????g++ test.cpp -o main -g -fno-elide-constructors????????????-fno-elide-constructors???????g++???????????????????????????????????????????????????????????????