对于原始类型,+运算符是否重载? [英] Is + operator overloaded for primitive types?

查看:165
本文介绍了对于原始类型,+运算符是否重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经重载的运算符<<,>>,=等多次使用。



我想到的一个例子是当我们添加字符串说:


string name = string(munish)+kumar


+运算符在字符串类中重载。



我们添加像1 + 2的数字(看起来不像一个重载的操作符调用)



我只是想知道它是如何发生的编译器做一个二进制加法。 / p>

我不需要担心它,虽然如果编译器这样做,只是一个好奇心为我。

operator + 这实际上是一个奇怪的名称的函数。基本类型的添加由CPU指令执行,例如:

  addl%edx,(%eax)//这将添加两在edx和eax加载的整数值

您实现 operator + 用于用户定义的类型,编译器会生成大量CPU指令来执行您在 operator + 中编写的任务。


Already overloaded operators <<,>>,= etc are used many times.

An example that i was thinking of is when we add strings say :

string name = string("munish") + "kumar";

the + operator is overloaded in the string class.

but when we add numbers like 1 + 2( doesn't seem like an overloaded operator call)

I was just wondering how does it happens does the compiler does a binary additon.

I don't need to worry about it much though if the compiler does it so, just a matter of curiosity for me.

解决方案

Primitive types don't implement operator+ which is actually a function with a weird name. Addition for Primitive type is carried out by CPU instruction such as :

addl %edx,(%eax) //this adds two integral values loaded at edx and eax

You implement operator+ for user-defined types, and compiler generates lots of CPU instructions to carry out the task which you write in operator+.

这篇关于对于原始类型,+运算符是否重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆