### C++总结-[类成员函数]

C++类中的常见函数。

#@author:       gr
#@date:         2015-07-23
#@email:        forgerui@gmail.com

一、constructor, copy constructor, copy assignment, destructor

1. copy constructor必须传引用,传值编译器会报错
2. operator= 返回值为引用,为了连续赋值
3. operator=需要进行自赋值判断
4. copy constructor, operator= 都需要调用子类的相应拷贝函数
5. operator=可以用copy constructor构造一个临时对象保存副本
6. destructor如果是基类,应该声明virtual
7. copy constructor对传进来的同类型对象的私有成员也有访问权限

private的限定符是编译器限定的,因为在类函数中,类私有成员变量是可见的,所以可以访问同类型私有成员。这个性质是针对所有类成员函数,不局限于构造函数。

class Widget {
private:
    char* name;
    int weight;
    static int counts;                  //需要在类外进行初始化
public:
    Widget(char* _name, int _weight) : name(new char[strlen(_name)+1]), weight(_weight) {   //初始化列表
        ++counts;
        strcpy(name, _name);
    }
    Widget(const Widget& lhs) : name(new char[strlen(lhs.name)+1]), weight(lhs.weight) {    //参数是引用类型
        ++counts;
        strcpy(name, lhs.name);
    }
    Widget& operator= (const Widget& lhs) {
        if (this == &lhs) {
            return *this;
        }
        Widget widTemp(lhs);        //临时对象
        char* pName = widTemp.name;
        widTemp.name = name;        //将被析构释放
        name = pName;
        ++counts;
        return *this;               //返回引用
    }
    virtual ~Widget() {             //基类析构声明为虚函数
        --counts;                   //静态变量减1
        delete[] name;              //释放指针
    }
};

class Television : public Widget {
private:
    int size;
public:
    Television(char* _name, int _weight, int _size) : Widget(_name, _weight), size(_size) {
    }
    Television(const Television& lhs) : Widget(lhs), size(lhs.size){
    }
    Television& operator= (const Television& lhs) {
        if (this == &lhs) {             //判断自赋值
            return *this;
        }
        size = lhs.size;
        Widget::operator=(lhs);         //调用子类
        return *this;                   //返回引用
    }
    ~Television() {
    }
};

二、operator+,operator+=

1. operator+需要返回const值传递
2. operator+= 需要返回引用,可以进行连续赋值
3. operator+ 可以调用operator+=实现
class Widget{
public:
    const Widget operator+ (const Widget& lhs) {        //返回const值
        Widget tempWidget(*this);
        tempWidget += lhs;                  //调用+=实现
        return tempWidget;
    }   
    Widget& operator+= (const Widget& lhs) {        //返回引用
        weight += lhs.weight;
        return *this;
    }
};

三、operator++ 前置、后置

1. 前置方式要返回引用
2. 后置方式返回值const,避免a++++形式出现
3. 为了区分两者,后置方式加一个int参数
4. 后置方式调用前置方式实现
class Widget {
public:
    const Widget operator++(int) {          //返回const值,添加一个int参数
        Widget tempWidget(*this);
        ++*this;                            //调用++实现
        return tempWidget;
    }
    Widget& operator++() {                  //返回引用
        weight++;           
        return *this;
    }
};

四、operator()

operator()一般可以实现函数对象,还可以用来模拟实现Matrix的元素获取。

函数对象需要实现operator(),使一个对象表现得像一个函数。

struct Compare: public binary_function<Widget, Widget, bool> {
public:
    bool operator() (const Widget& rhs, const Widget& lhs)const {
        return rhs.weight < lhs.weight;
    }
};

Widget a("a", 1);
Widget b("b", 2);
//下面使用Compare形式像函数
std::cout << Compare(a, b) ? "a < b" : "a >= b" << std::endl;

转载于:https://www.cnblogs.com/gr-nick/p/4753133.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值