zoukankan      html  css  js  c++  java
  • C++ 类型转化(运算符重载函数)和基本运算符重载(自增自减)

    类型转化(运算符重载函数)

    用转换构造函数可以将一个指定类型的数据转换为类的对象。但是不能反过来将一个类的对象转换为一个其他类型的数据(例如将一个Complex类对象转换成double类型数据)。在C++提供类型转换函数(type conversion function)来解决这个问题。类型转换函数的作用是将一个类的对象转换成另一类型的数据。

    类型转换函数的一般形式为:

    operator 类型名( ){
    	实现转换的语句
    }
    

    下面是简单实现。这时候,Base起了两方面的作用:类和数据类型。系统会在需要的时候自动调用对应的类方法。

    #include <iostream>
    using namespace std;
    
    class Base{
    	private:
    		float  x;
    		int y;
    	public:
    		Base (float xx=0,int yy=0){
    			x = xx;
    			y = yy;
    		}
    		operator float (){
    			return x;
    		}
    		operator int (){
    			return y;
    		}
    		void display(){
    			cout<<"x is :"<<x<<";y is :"<<y<<endl;
    		}
    };
    
    int main()
    {
    	Base base(1.0,2);
    	base.display();
    	int y= base;
    	float x= base;
    	cout<<"NewX is :"<<x<<"NewY is:"<<y<<endl;
        return 0;
    }
    

    基本运算符重载(自增自减)

    主要总结 自增自减的前置和后置的用法。其他的加减乘除较简单。

    简单的代码实现(纯语法)

    #include <iostream>
    using namespace std;
    
    class Base{
    	private:
    		float  x;
    		int y;
    	public:
    		Base (float xx=0,int yy=0){
    			x = xx;
    			y = yy;
    		}
    		operator float (){
    			return x;
    		}
    		operator int (){
    			return y;
    		}
    		Base operator ++(){//前置 ++
    			x++;
    			y++;
    			return *this;
    		} 
    		Base operator --(){
    			x--;
    			y--;
    			return *this;
    		}
    		Base operator ++(int ){//后置 ++
    			Base temp = *this;
    			++(*this);
    			return temp;
    		}
    		Base operator --(int ){
    			Base temp = *this;
    			--(*this);
    			return temp;
    		}
    		void display(){
    			cout<<"x is :"<<x<<";y is :"<<y<<endl;
    		}
    		
    };
    
    int main()
    {
    	Base base(1.0,1);
    	Base tem = base++;
    	base.display();
    	tem.display(); 
    	
    	Base base2(1.0,1);
    	tem = ++base2;
    	base.display();
    	tem.display(); 
        return 0;
    }
    

    发现:

    1. 后置和前置的区别是有无int参数。
    2. 后置需要申请新的空间,大小是类的大小。所以,后置操作会有额外的时间空间开销。
    3. 尽量使用前置操作:如:for (int i=0;i<n;++i)
  • 相关阅读:
    自己动手,制作真正的.net Framework 3.5 SP1离线安装包(转)
    理解lvalue和rvalue
    Android webView 支持缩放及自适应屏幕
    微信打赏小程序寻投资或买断代码
    微信批量关注公众号,推送消息软件介绍
    QQ微信号数据库 微信号采集 可区分性别地区1-100万个
    给客户开发的 贵金属喊单视频直播间 财经直播间系统
    揭秘:各大视频网站是这样密谋秀场的
    类似YY 9158网页版多人语音视频聊天室远程教学系统源码
    WEB网页视频会议系统源码
  • 原文地址:https://www.cnblogs.com/AsuraDong/p/6974256.html
Copyright © 2011-2022 走看看