一般操作符重载和friend重载

news/2025/2/22 16:55:50
cle class="tags" href="/tags/CLASS.html" title=class>class="baidu_pl">
cle_content" class="tags" href="/tags/CLASS.html" title=class>class="article_content clearfix">
content_views" class="tags" href="/tags/CLASS.html" title=class>class="htmledit_views">

class="tags" href="/tags/CLASS.html" title=class>class="Apple-style-span" style="TEXT-ALIGN: center; WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: medium 宋体; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: #000000; WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">class="tags" href="/tags/CLASS.html" title=class>class="Apple-style-span" style="TEXT-ALIGN: left; LINE-HEIGHT: 22px; FONT-FAMILY: Arial, Helvetica, sans-serif; COLOR: #2e3c78; FONT-SIZE: 14px">

转自:http://linpder.blog.163.com/blog/static/487641020095231925257/

 

运算符在重载的时候有两种方式࿰c;一是成员函数方式c;二是成友元(friend)函数方式class="tags" href="/tags/CLASS.html" title=class>class="Apple-converted-space"> 
成员函数比较简单。下面是一个示例代码:class="tags" href="/tags/CLASS.html" title=class>class="Apple-converted-space"> 

如果是设置成友元函数࿰c;一定要注意:class="tags" href="/tags/CLASS.html" title=class>class="Apple-converted-space"> 
color: #0000ff;">(1)当重载友元函数时࿰c;将没有隐含的参数this指针。这样࿰c;对双目运算符࿰c;友元函数有2个参数࿰c;对单目运算符࿰c;友元函数有一个参数。class="tags" href="/tags/CLASS.html" title=class>class="Apple-converted-space"> 
(2)有些运行符不能重载为友元函数࿰c;它们是:color: #ff8040;">=,(),[]和->class="tags" href="/tags/CLASS.html" title=class>class="Apple-converted-space"> 
因此࿰c;上面那个+运算符如果是重载为友元函数的话࿰c;应该写成:class="tags" href="/tags/CLASS.html" title=class>class="Apple-converted-space"> 

 

还需要注意一点࿰c;有的VC 6.0class="tags" href="/tags/BianYiQi.html" title=编译器>编译器在编译上面代码的时候会报错࿰c;错误为:class="tags" href="/tags/CLASS.html" title=class>class="Apple-converted-space"> 
color: #ff0000;">fatal error C1001: INTERNAL COMPILER ERRORclass="tags" href="/tags/CLASS.html" title=class>class="Apple-converted-space"> 
原因是VCclass="tags" href="/tags/BianYiQi.html" title=编译器>编译器本身的问题。class="tags" href="/tags/CLASS.html" title=class>class="Apple-converted-space"> 
解决办法:class="tags" href="/tags/CLASS.html" title=class>class="Apple-converted-space"> 在类的声明之前再加上一个声明。具体代码如下:class="tags" href="/tags/CLASS.html" title=class>class="Apple-converted-space"> 

 

这样就没有问题了。

cle>

http://www.niftyadmin.cn/n/619900.html

相关文章

引用传参和指针传参的差别.

在C中,如果函数的实参的类型是数据类型比较大的数据类型,这是如果使用一般传参就会有很大的不方便,这是如果能够传递一个地址或者是对原来参数的一个引用对提高性能会有很大的帮助。 本文研究一下引用传参和指针传参的区别和联系。其实引用传…

关于C++中函数返回引用的讨论

从函数返回引用确保其引用的对象在函数执行完后仍然存在。引用类型返回值的主要特征是可以作为左值,这意味着我们可以在赋值语句的左边使用返回引用的函数的结果。永远不要从函数中返回局部变量的引用.(1)首先,返回引用&#xff0…

用 const 限定类的成员函数

http://blog.csdn.net/whyglinux/archive/2006/02/18/602329.aspx 类的成员函数后面加 const,表明这个函数不会对这个类对象的数据成员(准确地说是非静态数据成员)作任何改变。 在设计类的时候,一个原则就是对于不改变数据成员的成…

什么时候用引用.

1. 什么是“引用”?申明和使用“引用”要注意哪些问题? 引用就是某个目标变量的“别名”(alias),对应用的操作与对变量直接操作效果完全相同。 申明一个引用的时候,切记要对其进行初始化。引用声明完毕后,相当于目标…

将两个unsigned short 合并成一个 unsigned int的方法.

void main(){unsigned short a[]{0xffff,0xeeee};unsigned short *s_pa;cout<<s_p[0]<<" "<<s_p[1]<<endl;unsigned int *p_int(unsigned int *)s_p;cout<<*p_int<<endl;//此输出结果相当于eeeeffff}

阅读笔记

1. char *p"world";//p指向的是常量字符串&#xff0c;常量字符串的内容是不能修改的。 p[0]X;//企图修改常量字符串&#xff0c;编译器不能发现错误&#xff0c; 运行时会报错。 #c/c语言没有办法知道指针所指的内存容量&#xff0c;除非在申请内存时记住它。 #当…

c++继承中的内存布局

今天在网上看到了一篇写得非常好的文章&#xff0c;是有关c类继承内存布局的。看了之后获益良多&#xff0c;现在转在我自己的博客里面&#xff0c;作为以后复习之用。 ——谈VC对象模型&#xff08;美&#xff09;简.格雷程化 译 译者前言 一个C程序员&#xff0c;想要进…

C++继承中的内存布局阅读笔记

2010-12-231.以字母类为例&#xff1a; class A{public:int a;};class B{public:int b;};class C:public A, public B{public:int c;};void main(){C c;void *p1(void *)&c;//此处将c对象的地址转换为void类型的指针方便比较void *p2(void *)(B *)&c;//先将其转换为B类…