操作符重载和复制构造函数

操作符重载和复制构造函数,右++是有参数的,左++是没有参数的。

#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <list>
#include <deque>
#include <iostream>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <string>
using namespace std;

class Point   
{   
private:   
    int x;  
public:   
    Point(int x1) 
    {   x=x1;}   
    Point operator++();//成员函数定义自增  
    const Point operator++(int x); //后缀可以返回一个const类型的值  
    friend Point operator--(Point& p);//友元函数定义--  
    friend const Point operator--(Point& p,int x);//后缀可以返回一个const类型的值
 Point(const Point &C){x=C.x;}

 Point operator=(Point C){
  printf("hello\n");
  Point *cc=new Point(C.x);
  return *cc;
 }
 int show(){return x;}
};   
 
Point Point::operator++()//++obj  
{
 printf("left ++\n");
    x++; 
    return *this; 

const Point Point::operator++(int x)//obj++  

 printf("right ++\n");
    Point temp = *this; 
    this->x++; 
    return temp; 

Point operator--(Point& p)//--obj  

    p.x--; 
    return p; 
         //前缀形式(--obj)重载的时候没有虚参,通过引用返回*this 或 自身引用,也就是返回变化之后的数值  

const Point operator--(Point& p,int x)//obj--  

    Point temp = p; 
    p.x--; 
    return temp; 
         // 后缀形式obj--重载的时候有一个int类型的虚参, 返回原状态的拷贝  


int main()
{
 Point a(1); 
 Point b(0);
 Point c(0);
 a++;//隐式调用成员函数operator++(0),后缀表达式  
 ++a;//隐式调用成员函数operator++(),前缀表达式  
 b--;//隐式调用友元函数operator--(0),后缀表达式  
 --b;//隐式调用友元函数operator--(),前缀表达式  
/*
 printf("a.data=%d\n",a.show());
 b=a++;
 printf("a.data=%d\n",b.show());
 c=++a;
 printf("a.data=%d\n",c.show());
*/
 
/*
 cout<<a.operator ++(2);//显式调用成员函数operator ++(2),后缀表达式  
 cout<<a.operator ++();//显式调用成员函数operator ++(),前缀表达式  
 cout<<operator --(b,2);//显式调用友元函数operator --(2),后缀表达式  
 cout<<operator --(b);//显式调用友元函数operator --(),前缀表达式 </PRE>
*/
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值