struct实现类定义

C++赋值运算符重载,为什么要返回引用?

adam posted @ 2010年11月27日 18:35 in C/C++ , 1655 阅读

 

一、c/c++赋值运算符的本意为“返回左值的引用”(左值:赋值号左面的变量而非其值)

例:

int a, b = 3, c = 2;
    (a = b) = c;
     cout<<a<<endl;

对于a = b(a,b均为对象时),若不返回左值的引用,将会生成临时对象。如果不处理a = b = c这样的表达式,也会正常(只是会调用拷贝构造函数和析构函数处理临时对象)。

二、为了进行连续赋值,即 x = y = z

1、赋值返回引用

x = y = z  先执行y = z,返回y的引用,执行x = y

2、赋值不返回引用

x = y = z  先执行y = z,返回用y初始化的临时对象(注意临时对象都是常对象),再执行x = y的临时对象(要求operator=(const X&)  ),返回用x初始化的临时对象(此处要求拷贝构造函数必须为X(const X&)  )。

所以也并非必须返回引用,返回引用的好处既可以于赋值的原始语义已知,又可避免拷贝构造函数和析构函数的调用。

“题外话”:如果在类中没有说明本身的拷贝构造函数和赋值运算符,编译程序将会提供,但他们都只是对对象进行成员浅拷贝。在那些指向堆空间指针作为数据成员的类中,必须避免使用浅拷贝,而要为类定义自己的赋值运算符,以给对象分配堆内存。

PS:浅拷贝:调用系统默认的拷贝构造函数,不再新分配资源内存。深拷贝:调用自己的拷贝构造函数,分配新的资源内存。

拷贝构造函数用已存在的对象创建一个相同的新对象。而赋值运算符用已存在的对象赋予一个已存在的同类对象。

Avatar_small
civaget 说:
2023年12月11日 23:46

러시아마사지 on a business trip? Count me in! It's the ultimate way to unwind during travels.

Avatar_small
civaget 说:
2023年12月12日 03:31

Remember, 구글 상위노출 isn't just about rankings; it's about establishing your online authority and showcasing your expertise.

Avatar_small
civaget 说:
2023年12月12日 19:41 Don't miss out on 제주오피 when visiting Jeju. It's the gateway to experiencing the island's magic.
Avatar_small
civaget 说:
2023年12月14日 23:59 Nice one for taking a few minutes to talk about this amazing, I think highly concerning it as well as absolutely love browsing regarding the issue. Any time prospects, because you gain skill, what views posting to the journal by having added data? This is very helpful for everyone. 해외축구중계
Avatar_small
civaget 说:
2023年12月18日 22:40

Exploring a new city? Don't forget to check out local 출장샵 options for a memorable wellness journey.

Avatar_small
civaget 说:
2023年12月19日 01:35

누누티비's content diversity makes it suitable for family viewing.

Avatar_small
civaget 说:
2023年12月19日 20:41

I'm a 천안출장마사지 enthusiast! It's improved my quality of life significantly.

Avatar_small
civaget 说:
2023年12月20日 18:46

I'm addicted to 뉴토끼 and its never-ending supply of webtoon previews and updates.

Avatar_small
civaget 说:
2023年12月22日 00:53

I can't imagine my work without 오피뷰's user-friendly interface.

Avatar_small
civaget 说:
2023年12月24日 18:29

I've lost count of the hours I've spent exploring new webtoons on 뉴토끼 – it's truly addictive!

Avatar_small
civaget 说:
2023年12月24日 21:34

Don't empty your wallet for sports - 무료스포츠중계 has got you covered.

Avatar_small
civaget 说:
2023年12月26日 22:03

There couple of fascinating points with time in this post but I don’t know if these center to heart. There’s some validity but I am going to take hold opinion until I explore it further. Excellent article , thanks and we want more! Added to FeedBurner likewise 프리카지노

Avatar_small
civaget 说:
2023年12月27日 04:36

The rise of self-publishing has fostered a sense of community among indie authors. self publishing on amazon

Avatar_small
civaget 说:
2023年12月31日 13:50

This is getting a bit more subjective, but I much prefer the Zune Marketplace. The interface is colorful, has more flair, and some cool features like ‘Mixview’ that let you quickly see related albums, songs, or other users related to what you’re listening to. Clicking on one of those will center on that item, and another set of “neighbors” will come into view, allowing you to navigate around exploring by similar artists, songs, or users. Speaking of users, the Zune “Social” is also great fun, letting you find others with shared tastes and becoming friends with them. You then can listen to a playlist created based on an amalgamation of what all your friends are listening to, which is also enjoyable. Those concerned with privacy will be relieved to know you can prevent the public from seeing your personal listening habits if you so choose. Divine Revelations

Avatar_small
civaget 说:
2023年12月31日 18:16

I can always count on 오피가이드 to provide reliable massage information.

Avatar_small
civaget 说:
2024年1月02日 20:21

Real-time updates from OPGuide ensure a smooth 오피 experience.

Avatar_small
civaget 说:
2024年1月06日 22:42

Finding massage parlors nearby is easy thanks to 오피스타. It's a convenient solution.

Avatar_small
civaget 说:
2024年1月14日 18:13

I appreciate that 누누티비 최신 doesn't require credit card information; it's hassle-free.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter