C++笔记 第四十课 前置操作符和后置操作符---狄泰学院

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42187898/article/details/84232606

如果在阅读过程中发现有错误,望评论指正,希望大家一起学习,一起进步。
学习C++编译环境:Linux

第四十课 前置操作符和后置操作符

1.值得思考的问题

下面的代码有没有区别?为什么?答案是5.真正的区别
i++; //i的值作为返回值,i自增1
++i; // i自增1,i的值作为返回值

40-1 真的有区别吗?

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int i = 0;
    i++;
    ++i;
    return 0;
}

2.意想不到的事实

现代编译器产品会对代码进行优化
优化是的最终的二进制程序更加高效
优化后的二进制程序丢失了C/C++的原生语义
不可能从编译后的二进制程序还原C/C++程序

3.思考

++操作符可以重载吗?如何区分前置++和后置++?

4.++操作符重载

++操作符可以被重载
全局函数和成员函数均可进行重载
重载前置++操作符不需要额外的参数
重载后置++操作符需要一个int类型的占位参数

40-2 ++操作符的重载

#include <iostream>
#include <string>
using namespace std;
class Test
{
    int mValue;
public:
    Test(int i)
    {
        mValue = i;
    }
    
    int value()
    {
        return mValue;
    }
    //前置++
    Test& operator ++ ()
    {
        ++mValue;
        
        return *this;
    }
    //后置++
    Test operator ++ (int)
    {
        Test ret(mValue);
        
        mValue++;
        
        return ret;
    }
};
int main()
{
    Test t(0);
    //下面两行意义不同,且前置++效率高,不需要构造函数和析构函数
    t++;
    
    ++t;
    
    return 0;
}

5.真正的区别

对于基础类型的变量
前置++的效率与后置++的效率基本相同
根据项目组编码规范进行选择
对于类类型的对象
前置++的效率高于后置++
尽量使用前置++操作符提供程序效率
class Complex 复数类的进一步改善

test.cpp
#include<iostream>
#include<string>
#include "Complex.h"
using namespace std;
int main()
{
    Complex c(0,0);
    c++;
    cout << c.getA() << endl;
    cout << c.getB() << endl;
    return 0;
}
Complex.h
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
class Complex
{
    double a;
    double b;
public:
    Complex(double a = 0, double b = 0);
    double getA();
    double getB();
    double getModulus();
    
    Complex operator + (const Complex& c);
    Complex operator - (const Complex& c);
    Complex operator * (const Complex& c);
    Complex operator / (const Complex& c);
    
    bool operator == (const Complex& c);
    bool operator != (const Complex& c);
    
    Complex& operator = (const Complex& c);
    
    Complex& operator ++ ();
    Complex operator ++ (int);
};
#endif
Complex.cpp
#include "Complex.h"
#include "math.h"
Complex::Complex(double a, double b)
{
    this->a = a;
    this->b = b;
}
double Complex::getA()
{
    return a;
}
double Complex::getB()
{
    return b;
}
double Complex::getModulus()
{
    return sqrt(a * a + b * b);
}
Complex Complex::operator + (const Complex& c)
{
    double na = a + c.a;
    double nb = b + c.b;
    Complex ret(na, nb);
    
    return ret;
}
Complex Complex::operator - (const Complex& c)
{
    double na = a - c.a;
    double nb = b - c.b;
    Complex ret(na, nb);
    
    return ret;
}
Complex Complex::operator * (const Complex& c)
{
    double na = a * c.a - b * c.b;
    double nb = a * c.b + b * c.a;
    Complex ret(na, nb);
    
    return ret;
}
Complex Complex::operator / (const Complex& c)
{
    double cm = c.a * c.a + c.b * c.b;
    double na = (a * c.a + b * c.b) / cm;
    double nb = (b * c.a - a * c.b) / cm;
    Complex ret(na, nb);
    
    return ret;
}
    
bool Complex::operator == (const Complex& c)
{
    return (a == c.a) && (b == c.b);
}
bool Complex::operator != (const Complex& c)
{
    return !(*this == c);
}
    
Complex& Complex::operator = (const Complex& c)
{
    if( this != &c )
    {
        a = c.a;
        b = c.b;
    }
    
    return *this;
}
Complex& Complex::operator ++ ()
{
    a = a + 1;
    b = b + 1;
    
    return *this;
}
    
Complex Complex::operator ++ (int)
{
    Complex ret(a, b);
    
    a = a + 1;
    b = b + 1;
    
    return ret;
}
运行结果
lkk@lkk-virtual-machine:~/c++/40$ g++ test.cpp Complex.cpp
lkk@lkk-virtual-machine:~/c++/40$ ./a.out
1
1

小结
编译优化使得最终的可执行程序更加高效
前置++操作符和后置++操作符都可以被重载
++操作符的重载必须符合其原生语义
对于基础类型,前置++与后置++的效率几乎相同
对于类类型前置++的效率高于后置++

猜你喜欢

转载自blog.csdn.net/weixin_42187898/article/details/84232606
今日推荐