在构造函数中使用new时的注意事项:
1.如果在构造函数中使用new来初始化指针成员, 则应该在析构函数中使用delete
2.new和delete必须相互兼容, new对应于delete, new[]对应于delete[]
3.如果有多个构造函数, 则必须以相同的方式使用new, 要么都带中括号, 要么都不带. 因为只有一个析构函数, 所有的构造函数都必须与它兼容. 然而可以在一个构造函数中使用new初始化指针, 而在另一个构造函数中将指针初始化为空(0或c++11中的nullptr), 因为delete(无论是带[], 还是不带[])都可以用于空指针.
4.应定义一个复制构造函数, 通过深度复制将一个对象初始化为另一个对象, 例如:
String::String(const String & st)
{
// 如果有必要, 处理静态的成员变量
num_strings++;
len = st.len;
// 分配足够的空间来存储数据
str = new char[len + 1];
// 复制数据
std::strcpy(str, st.str);
}
5.应当定义一个赋值运算符, 通过深度复制讲一个对象复制给另一个对象, 例如:
String & String::operator=(const String & st)
{
// this是指向本类对象的指针
// 防止由于赋值给对象本身, 不加判断的话, 会执行delete会把原有的数据删除
if(this == &st)
return *this;
// 删除原有存储的数据
delete[] str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
}
包含类成员的类的逐成员复制:
假设类成员的类型为String类或标准string类:
class Magazine
{
private:
String title;
string publisher;
...
}
String和string都使用动态内存分配, 但是这个并不意味着需要为Magazine类编写复制构造函数和赋值运算符, 默认的逐成员复制或赋值行为有一定的智能: 如果将Magazine对象复制或赋值给另一个Magazine对象, 逐成员复制将使用成员类型定义的复制构造函数和赋值运算符. 也就是说, 复制成员title时, 将使用String的复制构造函数, publisher将使用string的复制构造函数. 但是如果Magazine类因其他成员需要定义复制构造函数和赋值运算符, 情况将变得复杂
有一点需要注意的, 假设Vector是一个自定义的类:
Vector Max(const Vector & v1, const Vector &2)
{
if(v1.magval() > v2.magval())
return v1;
else
return v2;
}
这个函数在返回的时候会调用复制构造函数, 而下面这中写法不对调用, 是因为返回的就是const Vector &, 返回的是const 引用
const Vector & Max(const Vector & v1, const Vector &2)
{
if(v1.magval() > v2.magval())
return v1;
else
return v2;
}
看一个使用对象指针的例子:
// string1.h
#ifndef STRING1_H_
#define STRING1_H_
#include <iostream>
using std::ostream;
using std::istream;
class String
{
private:
char * str;
int len;
static int num_strings;
// cin输入限制
static const int CINLIM = 80;
public:
// 构造函数
String(const char * s);
// 默认构造函数
String();
// 复制构造函数
String(const String &);
// 析构函数
~String();
// 定义内带实现, 就是内联函数
int length() const {return len;}
// 重载操作运算符
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
// 友元函数, 重载操作运算符
friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st, const String &st2);
friend bool operator==(const String &st, const String &st2);
friend ostream & operator<<(ostream & os, const String &st);
friend istream & operator>>(istream & is, String &st);
// 静态方法
static int HowMany();
};
#endif
// 第二个文件:
// string1.cpp
#include <cstring>
#include "string1.h"
using std::cin;
using std::cout;
// 初始化静态成员
int String::num_strings = 0;
// 静态方法:
int String::HowMany()
{
return num_strings;
}
// 类方法
// 构造函数
String::String(const char * s)
{
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
num_strings++;
}
// 默认构造函数
String::String()
{
len = 4;
str = new char[1];
str[0] = '\0';
num_strings++;
}
// 构造函数
String::String(const String & st)
{
num_strings++;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
}
// 析构函数
String::~String()
{
--num_strings;
delete[] str;
cout << " ------------delete----------" << std::endl;
}
// 重载操作运算符
// 将一个String对象赋值给一个String
String & String::operator=(const String & st)
{
if(this == &st)
return *this;
delete[] str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
}
// 将一个char型指针, 赋值给一个String对象
String & String::operator=(const char * s)
{
delete[] str;
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
return *this;
}
char & String::operator[](int i)
{
return str[i];
}
const char & String::operator[](int i) const
{
return str[i];
}
// 友元函数
bool operator<(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) < 0);
}
bool operator>(const String &st1, const String &st2)
{
return st2 < st1;
}
bool operator==(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) == 0);
}
ostream & operator<<(ostream & os, const String & st)
{
os << st.str;
return os;
}
istream & operator>>(istream & is, String & st)
{
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if(is)
st = temp;
while(is && is.get() != '\n')
continue;
return is;
}
// sayings2.cpp
// compile with string1.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "string1.h"
const int ArSize = 10;
const int MaxLen = 81;
int main()
{
using namespace std;
String name;
cout << "Hi, what's your name " << endl;
cin >> name;
cout << name << ", please enter up to " << ArSize << " short sayings <empty line to quit>: " << endl;
String sayings[ArSize];
char temp[MaxLen];
int i;
for(i = 0; i < ArSize; i++)
{
cout << i + 1 << ": ";
cin.get(temp, MaxLen);
while(cin && cin.get() != '\n')
continue;
if(!cin || temp[0] == '\0')
break;
else
sayings[i] = temp;
}
int total = i;
if(total > 0)
{
cout << "Here are your sayings: " << endl;
for(i = 0; i < total; i++)
{
cout << sayings[i] << endl;
}
// 使用指针跟踪最短的字符串
String * shortest = &sayings[0];
// 使用指针来跟踪字母顺序最靠前的字符串
String * first = &sayings[0];
for(i = 1; i < total; i++)
{
if(sayings[i].length() < shortest->length())
shortest = &sayings[i];
if(sayings[i] < *first)
first = &sayings[i];
}
cout << "Shortest saying: " << endl << *shortest << endl;
cout << "First alaphabetically: " << endl << *first << endl;
srand(time(0));
int choice = rand() % total;
String * favorite = new String(sayings[choice]);
cout << "My favorite saying: " << endl << *favorite << endl;
delete favorite;
}
else
cout << "Not much to say " << endl;
cout << "Bye" << endl;
return 0;
}
程序运行结果为: