C++11?е????????
???????????? ???????[ 2015/8/24 10:45:59 ] ??????????????????? .NET
??????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++???????????????????????????????????????????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11