前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >从零开始学C++之运算符重载(二):++运算符重载、!运算符重载、赋值运算符重载

从零开始学C++之运算符重载(二):++运算符重载、!运算符重载、赋值运算符重载

作者头像
s1mba
发布2017-12-28 17:05:13
1.1K0
发布2017-12-28 17:05:13
举报
文章被收录于专栏:开发与安全开发与安全

一、++运算符重载

前置++运算符重载

成员函数的方式重载,原型为: 函数类型 & operator++();

友元函数的方式重载,原型为: friend 函数类型 & operator++(类类型 &);

后置++运算符重载

成员函数的方式重载,原型为: 函数类型  operator++(int);

友元函数的方式重载,原型为: friend 函数类型  operator++(类类型 &, int);

代码语言:cpp
复制
#ifndef _INTEGER_H_
#define _INTEGER_H_

class Integer
{
public:
    Integer(int n);
    ~Integer();

    Integer &operator++();
    //friend Integer& operator++(Integer& i);

    Integer operator++(int n);
    //friend Integer operator++(Integer& i, int n);

    void Display() const;
private:
    int n_;
};

#endif // _INTEGER_H_
代码语言:cpp
复制
#include "Integer.h"
#include <iostream>
using namespace std;

Integer::Integer(int n) : n_(n)
{
}

Integer::~Integer()
{
}

Integer &Integer::operator ++()
{
    //cout<<"Integer& Integer::operator ++()"<<endl;
    ++n_;
    return *this;
}

//Integer& operator++(Integer& i)
//{
//  //cout<<"Integer& operator++(Integer& i)"<<endl;
//  ++i.n_;
//  return i;
//}

Integer Integer::operator++(int n)
{
    //cout<<"Integer& Integer::operator ++()"<<endl;
    //n_++;
    Integer tmp(n_);
    n_++;
    return tmp;
}

//Integer operator++(Integer& i, int n)
//{
//  Integer tmp(i.n_);
//  i.n_++;
//  return tmp;
//}

void Integer::Display() const
{
    cout << n_ << endl;
}
代码语言:cpp
复制
#include "Integer.h"
#include <iostream>
using namespace std;


int main(void)
{
    Integer n(100);
    n.Display();

    Integer n2 = ++n;
    n.Display();
    n2.Display();

    Integer n3 = n++;
    n.Display();
    n3.Display();


    return 0;
}

需要注意的是为了区别于前置++,后置++多了一个int 参数,但实际上是没作用的,设置断点调试的时候可以发现默认赋值为0。

而且此时成员函数不能与友元函数共存,因为调用++运算符时不明确。

二、赋值运算符重载、!运算符重载

代码语言:cpp
复制
#ifndef _STRING_H_
#define _STRING_H_

class String
{
public:
    explicit String(const char *str = "");
    String(const String &other);
    String &operator=(const String &other);
    String &operator=(const char *str);

    bool operator!() const;
    ~String(void);

    void Display() const;

private:
    char *AllocAndCpy(const char *str);
    char *str_;
};

#endif // _STRING_H_
代码语言:cpp
复制
#pragma warning(disable:4996)
#include "String.h"
#include <string.h>
#include <iostream>
using namespace std;

String::String(const char *str)
{
    str_ = AllocAndCpy(str);
}

String::String(const String &other)
{
    str_ = AllocAndCpy(other.str_);
}

String &String::operator=(const String &other)
{
    if (this == &other)
        return *this;

    delete[] str_;
    str_ = AllocAndCpy(other.str_);
    return *this;
}

String &String::operator=(const char *str)
{
    delete[] str_;
    str_ = AllocAndCpy(str);
    return *this;
}

bool String::operator!() const
{
    return strlen(str_) != 0;
}

String::~String()
{
    delete[] str_;
}

char *String::AllocAndCpy(const char *str)
{
    int len = strlen(str) + 1;
    char *newstr = new char[len];
    memset(newstr, 0, len);
    strcpy(newstr, str);

    return newstr;
}

void String::Display() const
{
    cout << str_ << endl;
}
代码语言:cpp
复制
#include "String.h"
#include <iostream>
using namespace std;


int main(void)
{
    String s1("abc");
    String s2(s1);

    String s3;
    s3 = s1;
    s3.Display();

    s3 = "xxxx";
    s3.Display();

    String s4;
    bool notempty;
    notempty = !s4;
    cout << notempty << endl;

    s4 = "aaaa";
    notempty = !s4;
    cout << notempty << endl;

    return 0;
}

需要注意的是我们将构造函数声明为explicit,故s3 = "xxxx"; 不能将"xxxx" 先隐式转换成临时String再调用 String &operator=(const String &other);,

可以再重载一个 String& operator=(const char* str); 函数。!运算符这里指当字符串不为空时为真。

参考:

C++ primer 第四版 Effective C++ 3rd C++编程规范

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-07-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档