如何定义一个完整的类
作者:网络转载 发布时间:[ 2012/10/12 9:47:29 ] 推荐标签:
11、删除数组时记住用delete[]了吗?
在删除任何类型的数组时使用delete[]是一种很好的习惯。
12、在复制构造函数和赋值构造函数的参数类型中加上const了吗?
这是某些 C++ 著作中也会犯的错误!
如果函数有引用参数,只有在函数想改变函数的输入参数时,才应该不用 const 声明该引用参数!通常这个会被改变的输入参数也肩负着输出参数的角色。
13、记得适当地声明成员函数为const了吗?
如果确信一个成员函数不用修改它的对象,可以声明它为 const。
14、一个需要上述所有特性类定义的例子
// 声明
class A{
public:
A();
virtual ~A();
A(const A& s);
A& operator=(const A& s);
bool operator==(const A& s) const;
bool operator!=(const A& s) const;
bool operator<(const A& s) const;
};
// 实现
A::A(){
// todo...
}
A::~A(){
// todo...
}
A::A(const A& s){
// todo...
}
A& A::operator=(const A& s){
if(&s != this){
// todo...
}
return *this;
}
bool A::operator==(const A& s) const{
// todo...
return false;
}
bool A::operator!=(const A& s) const{
return !((*this)==s);
}
bool A::operator<(const A& s) const{
// todo...
return false;
}

sales@spasvo.com