#include "pch.h"
#include <iostream>
#include<string.h>
using namespace std;
class MyString
{
public:
char *buffer;
public:
MyString(const char * init)
{
buffer = NULL;
cout << "Default constructor: creating new MyString" << endl;
if (init != NULL)
{
buffer = new char[strlen(init) + 1];
strcpy(buffer, init);
cout << "buffer points to: 0x" << hex;
cout << (unsigned int*)buffer << endl;
}
}
~MyString()
{
cout << "Invoking destructor, clearing up" << endl;
delete[] buffer;
}
int GetLength()
{
return strlen(buffer);
}
const char *GetString()
{
return buffer;
}
};
int main()
{
MyString sayHello("Hello from String Class");
MyString copysay = sayHello;
cout << (unsigned int *)sayHello.buffer << endl;
cout << (unsigned int *)copysay.buffer << endl;
cout << (unsigned int )*copysay.buffer << endl;
cout << *copysay.buffer << endl;
return 0;
}
结果:
Default constructor: creating new MyString
buffer points to: 0x0000025F1DB4AC50
0000025F1DB4AC50
0000025F1DB4AC50
48
H
Invoking destructor, clearing up
Invoking destructor, clearing up
#include "pch.h"
#include <iostream>
#include<string.h>
using namespace std;
class MyString
{
public:
char *buffer;
public:
MyString(const char * init)
{
buffer = NULL;
cout << "Default constructor: creating new MyString" << endl;
if (init != NULL)
{
buffer = new char[strlen(init) + 1];
strcpy(buffer, init);
cout << "buffer points to: 0x" << hex;
cout << (unsigned int*)buffer << endl;
}
}
~MyString()
{
cout << "Invoking destructor, clearing up" << endl;
delete[] buffer;
}
int GetLength()
{
return strlen(buffer);
}
const char *GetString()
{
return buffer;
}
};
void UseMystr(MyString str)
{
cout<< "String buffer in MyString is " << str.GetLength();
cout << " characters long" << endl;
cout << "buffer contains: " << str.GetString() << endl;
return;
}
int main()
{
MyString sayHello("Hello from String Class");
UseMystr(sayHello);
return 0;
}
- 新建对象Mystring str ,str中的buffer 和sayHello中的buffer 指向同一个内存空间,两次使用delete会出错。可以将UseMystr(MyString str)改为UseMystr(MyString & str)
#include "pch.h"
#include <iostream>
#include<string.h>
using namespace std;
class MyString
{
public:
char *buffer;
public:
MyString(const char * init)
{
buffer = NULL;
cout << "Default constructor: creating new MyString" << endl;
if (init != NULL)
{
buffer = new char[strlen(init) + 1];
strcpy(buffer, init);
cout << "buffer points to: 0x" << hex;
cout << (unsigned int*)buffer << endl;
}
}
~MyString()
{
cout << "Invoking destructor, clearing up" << endl;
delete[] buffer;
}
int GetLength()
{
return strlen(buffer);
}
const char *GetString()
{
return buffer;
}
};
void UseMystr(MyString &str)
{
cout<< "String buffer in MyString is " << str.GetLength();
cout << " characters long" << endl;
cout << "buffer contains: " << str.GetString() << endl;
return;
}
int main()
{
MyString sayHello("Hello from String Class");
UseMystr(sayHello);
cout << "test..." << endl;
return 0;
}
- 没有进行对象复制
Default constructor: creating new MyString
buffer points to: 0x0000021B0DBEAB30
String buffer in MyString is 17 characters long
buffer contains: Hello from String Class
test…
Invoking destructor, clearing up
#include "pch.h"
#include <iostream>
#include<string.h>
using namespace std;
class MyString
{
public:
char *buffer;
public:
MyString(const char * init)
{
buffer = NULL;
cout << "Default constructor: creating new MyString" << endl;
if (init != NULL)
{
buffer = new char[strlen(init) + 1];
strcpy(buffer, init);
cout << "buffer points to: 0x" << hex;
cout << (unsigned int*)buffer << endl;
}
}
MyString(const MyString& copySource) // Copy constructor
{
buffer = NULL;
cout << "Copy constructor: copying from MyString" << endl;
if (copySource.buffer != NULL)
{
// allocate own buffer
buffer = new char[strlen(copySource.buffer) + 1];
// deep copy from the source into local buffer
strcpy(buffer, copySource.buffer);
cout << "buffer points to: 0x" << hex;
cout << (unsigned int*)buffer << endl;
}
}
~MyString()
{
cout << "Invoking destructor, clearing up" << endl;
delete[] buffer;
}
int GetLength()
{
return strlen(buffer);
}
const char *GetString()
{
return buffer;
}
};
void UseMystr(MyString str)
{
cout<< "String buffer in MyString is " << str.GetLength();
cout << " characters long" << endl;
cout << "buffer contains: " << str.GetString() << endl;
return;
}
int main()
{
MyString sayHello("Hello from String Class");
//MyString copysay = sayHello;
MyString copysay(sayHello);
cout << (unsigned int *)copysay.buffer << endl;
cout << "test..." << endl;
return 0;
}
Default constructor: creating new MyString
buffer points to: 0x00000275FA7BB0B0
Copy constructor: copying from MyString
buffer points to: 0x00000275FA7BAD50
00000275FA7BAD50
test…
Invoking destructor, clearing up
Invoking destructor, clearing up