搜索
您的当前位置:首页正文

C++基础-(重载)

来源:二三娱乐

C++基础

重载

  1. 哪些运算符可以被重载:::,.,->,*,?:不能被重载
  2. 重载操作符的标志(operator)
#include <iostream>
using namespace std;
int a1=12,b1=13,c1;
class test
{
    int m_z;
    public:
        test(int z)
        {
            m_z=z;
        }
        test()
        {
            m_z=10;
        }
        int getz()
        {
            return m_z;
        }
        void setz(int z)
        {
            m_z=z;
        }
};
test a2(10),b2(5),c2;
test  operator+(test t1,test t2)
{
    return t1.getz()+t2.getz();
}
int main(int argc,char **argv)
{
    c1=a1+b1;//operator+(a1,b1);=>operator+(int i1,int i2);
    c2=a2+b2;//operator+(a2+b2);=>operator+(test t1,test t2);//这样编不过
    cout<<"c1="<<c1<<endl;
    cout<<"c2="<<c2.getz()<<endl;
}
/*
int operator+(test t1,test t2)
{
    return t1.getz()+t2.getz();
}
c1=a2+b2;//operator+(a2+b2);=>operator+(test t1,test t2);//这样编不过
cout<<"c1="<<c1<<endl;*/
#include <iostream>
using namespace std;
int a1=12,b1=13,c1;
class test
{
    int m_z;
    public:
        test(int z)
        {
            m_z=z;
        }
        test()
        {
            m_z=10;
        }
        int getz()
        {
            return m_z;
        }
        void setz(int z)
        {
            m_z=z;
        }
        int  operator+(test t2)//作为成员的运算符比之作为非成员的运算符,在声明和定义时,形式上少一个参数。
        {
            return  m_z+t2.m_z;
        }
};
test  a2(10),b2(5),c2; 
int main(int argc,char **argv)
{
    c1=a1+b1;//operator+(a1,b1);=>operator+(int i1,int i2);
    c1=a2+b2;//operator+(a2+b2);=>operator+(test t1,test t2);//这样编不过
    cout<<"c1="<<c1<<endl;
}
  • C++运算符规定
    • 重载运算符要保持原有运算符的意义
    • 只能对已有运算符重载
    • 重载的运算符不能改变原有的优先级与结和性
    • C++中参数说明都是内部运算符时不能重载。//int operator+(int a,int b)不可实现
  • 一个运算符还可以作为一个成员函数实现
  • 作为成员的运算符
  • 总结
    • 操作符重载把操作符的作用扩展到对象类型
    • 为了访问被保护的对象,需要把重载函数定义成友元
    • 操作符重载可以是成员函数
    • 前增量后增量通过一个形参区分
#include <iostream>
using namespace std;
int a1=12,b1=13,c1;
class test
{
    int m_z;
    public:
        test(int z)
        {
            m_z=z;
        }
        test()
        {
            m_z=10;
        }
        int getz()
        {
            return m_z;
        }
        void setz(int z)
        {
            m_z=z;
        }
        int  operator+(test t2)
        {
            return  m_z+t2.m_z;
        }
        friend test &operator++(test &t);//前缀++
        friend test operator++(test &t,int);//后缀++
        friend ostream &operator<<(ostream &c,test &z);
};
test  a2(10),b2(5),c2;
test &operator++(test &t)//前缀++
{
    ++t.m_z;
    return t;
}
test operator++(test &t,int)//后缀++
{
    test z(t.m_z);
    ++t.m_z;
    return z;
}
ostream &operator<<(ostream &c,test &z)
{
    c<<z.m_z;
    return c;
}
int main(int argc,char **argv)
{
    c2=++a2;
    cout<<"a2="<<a2.getz()<<endl;
    cout<<"c2="<<c2.getz()<<endl;
    cout<<a2<<endl;//operator<<(cout,a2);=>operator<<(ostream& ,test &);
}
  • 操作符只能作为成员函数重载
    • [],(),=
  • 重载总结:
    • 运算符重载必要性:增加可读性
    • 重载函数最好有相同类型
    • 不要返回一个局部对象的引用
    • 运算符作为成员函数
Top