目录

0. 前言

  c++默认赋值构造函数的返回值是引用类型,c++赋值运算符=的本意是返回左值的引用,我们重载赋值构造函数的时候,返回值是否应该设为引用类型呢? 按照《Effective C++》中第10条,最好是设为引用类型。

  本文,通过实验来表述返回值是否为引用类型的区别。

1. 内置类型

  int i= 1,     j=2,    k=3;

  • case1: k = j = i

    i == 1

  j == 1

  k == 1

  • case2: (k = j) = i

    i == 1

  j == 2

  k == 1

2. 自定义类型

  People p1("p1"),     p2("p2"),     p3("p3");

  • case1: p3 = p2 = p1
    • 使用默认赋值构造函数

      p3 == p1

    p2 == p1

    • 返回引用的People

      p3 == p1

  p2 == p1

    • 不返回引用的People

      p3 == p1

    p2 == p1

  • case2: (p3 = p2) = p1
    • 使用默认赋值构造函数

       p3 == p1

    p2 == p2

    • 返回引用的People

  p3 == p1

  p2 == p2

    • 不返回引用的People

      p3 == p2

    p2 == p2

3. 结论

  • case1,是否返回引用没有影响;
  • case2,是否返回引用是有区别的,由于c++内置类型的赋值重载操作符是返回引用的,所以我们应该遵循规则,类的赋值构造函数返回引用类型

4. 实验源码

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cwchar>
#include <functional>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <string>
#include <vector>
#include <memory> #include <sstream>
#include <utility> using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::stringstream; void test_int()
{
cout << "test_int()" << endl;
int i = , j = , k = ; k = j = i; cout << "\ti=" << i << endl; //
cout << "\tj=" << j << endl; //
cout << "\tk=" << k << endl; //
} void test_int2()
{
cout << "test_int2()" << endl;
int i = , j = , k = ; (k = j) = i; cout << "\ti=" << i << endl; //
cout << "\tj=" << j << endl; //
cout << "\tk=" << k << endl; //
} class People
{
public:
People(const string &_name = "")
: name(_name)
{} People operator=(const People &_p)
{
name = _p.name;
return *this;
} string name;
}; void test()
{
cout << "test(): not reference" << endl;
cout << "\tp3=p2=p1" << endl;
People p1("p1"), p2("p2"), p3("p3"); p3 = p2 = p1; cout << "\t\tp2.name=" << p2.name << endl; // p1
cout << "\t\tp3.name=" << p3.name << endl; // p1
} void test2()
{
cout << "test2(): not reference" << endl;
cout << "\t(p3=p2)=p1" << endl;
People p1("p1"), p2("p2"), p3("p3"); (p3 = p2) = p1; cout << "\t\tp2.name=" << p2.name << endl; // p2
cout << "\t\tp3.name=" << p3.name << endl; // p2
} class PeopleRef
{
public:
PeopleRef(const string &_name = "")
: name(_name)
{} PeopleRef& operator=(const PeopleRef &_p)
{
name = _p.name;
return *this;
} string name;
}; void test_ref()
{
cout << endl;
cout << "test_ref(): reference" << endl;
cout << "\tp3=p2=p1" << endl;
PeopleRef p1("p1"), p2("p2"), p3("p3");; p3 = p2 = p1; cout << "\t\tp2.name=" << p2.name << endl; // p1
cout << "\t\tp3.name=" << p3.name << endl; // p1
} void test_ref2()
{
cout << "test_ref2(): reference" << endl;
cout << "\t(p3=p2)=p1" << endl;
PeopleRef p1("p1"), p2("p2"), p3("p3");; (p3 = p2) = p1; cout << "\t\tp2.name=" << p2.name << endl; // p2
cout << "\t\tp3.name=" << p3.name << endl; // p1
} class PeopleDefault
{
public:
PeopleDefault(const string &_name = "")
: name(_name)
{} string name;
}; void test_default()
{
cout << endl;
cout << "test_default()" << endl;
cout << "\tp3=p2=p1" << endl;
PeopleDefault p1("p1"), p2("p2"), p3("p3"); p3 = p2 = p1; cout << "\t\tp2.name=" << p2.name << endl; // p1
cout << "\t\tp3.name=" << p3.name << endl; // p1
} void test_default2()
{
cout << "test_default2()" << endl;
cout << "\t(p3=p2)=p1" << endl;
PeopleDefault p1("p1"), p2("p2"), p3("p3"); (p3 = p2) = p1; cout << "\t\tp2.name=" << p2.name << endl; // p2
cout << "\t\tp3.name=" << p3.name << endl; // p1
} int main ()
{
test_int();
test_int2(); test();
test2(); test_ref();
test_ref2(); test_default();
test_default2();
}

此文章为 '永无止境'(入驻一点号的媒体名称)原创,特此声明!

c++赋值构造函数为什么返回引用类型?的更多相关文章

  1. C++ 赋值构造函数的返回值到底有什么用?且返回值是否为引用类型有什么区别吗?

    首先定义类Person class Person{ public: string name; Person()=default; //默认构造函数 Person(string nam):name(na ...

  2. c++类大四个默认函数-构造函数 析构函数 拷贝构造函数 赋值构造函数

    每个类只有一个析构函数和一个赋值函数,但可以有多个构造函数(包含一个拷贝构造函数,其它的称为普通构造函数).对于任意一个类A,如果不编写上述函数,C++编译器将自动为A 产生四个缺省的函数,例如: A ...

  3. C++ 赋值函数为什么返回reference to *this?

    赋值操作为什么要返回 reference to *this? 要弄清这个问题之前,先了解函数的返回值类型:返回值类型,返回引用类型 返回值类型:返回的是一个对象的副本. test operator= ...

  4. C++ 为什么拷贝构造函数参数必须为引用?赋值构造函数参数也必须为引用吗?

    之前写拷贝构造函数的时候,以为参数为引用,不为值传递,仅仅是为了减少一次内存拷贝.然而今天看到一篇文章发现自己对拷贝构造的参数理解有误. 参数为引用,不为值传递是为了防止拷贝构造函数的无限递归,最终导 ...

  5. (copy)赋值构造函数的4种调用时机or方法

    第一种调用方法: demo #include <iostream> using namespace std; class Text { public: Text() // 无参数构造函数 ...

  6. C++函数的返回值——返回引用类型&非引用类型

    函数的返回主要分为以下几种情况: 1.主函数main的返回值: 允许主函数main没有返回值就可结束:可将主函数main返回的值视为状态指示器,返回0表示程序运行成功,其他大部分返回值则表示失败. 2 ...

  7. C++类中函数(构造函数、析构函数、拷贝构造函数、赋值构造函数)

    [1]为什么空类可以创建对象呢? 示例代码如下: #include <iostream> using namespace std; class Empty { }; void main() ...

  8. C语言中赋值表达式的返回值是什么?

    我们或多或少都有过,或者见过将赋值表达式参与运算的情况.这通常会伴随着一些意想不到的问题.今天我就见到了一段奇怪的代码: #include<stdio.h> int main() { ; ...

  9. 【opencv 源码剖析】 四、 Mat的赋值构造函数 和 拷贝构造函数

    1.赋值构造函数 右值引用 inline Mat& Mat::operator = (Mat&& m) { if (this == &m) return *this; ...

随机推荐

  1. C#中Timer定时器的使用示例

    关于C#中timer类 在C#里关于定时器类就有3个: 1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类里 3.定义在System.Tim ...

  2. Oracle字段根据逗号分割查询数据

    需求是表里的某个字段存储的值是以逗号分隔开来的,要求根据分隔的每一个值都能查出来数据,但是不能使用like查询. 数据是这样的: 查询的sql如下: select * from ( select gu ...

  3. Jquery EasyUI Combotree和 EasyUI tree展开所有父节点和获取完整路径

    Jquery EasyUI Combotree展开所有父节点 Jquery EasyUI Combotree获取树完整路径 Jquery EasyUI tree展开所有父节点 Jquery EasyU ...

  4. jQuery UI =>jquery-ui.js中sortable方法拖拽对象位置偏移问题

    今天要处理sortable方法处理的对象,拖拽的时候,位置偏移的问题. 按理应该是鼠标在哪,对象就跟着在哪的 百度了一下问题,http://blog.csdn.net/samed/article/de ...

  5. Oracle RAC环境下ASM磁盘组扩容

    生产环境注意调整以下参数: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  6. Springboot 文件上传(带进度条)

    1. 相关依赖 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...

  7. Oracle同义词(synonym)

    oracle的同义词总结   从字面上理解就是别名的意思,和视图的功能类似.就是一种映射关系.   同义词拥有如下好处:   节省大量的数据库空间,对不同用户的操作同一张表没有多少差别;   扩展的数 ...

  8. 阅历>感悟

    1.强扭的瓜不甜.在招聘的时候,面试官看不上你,你也不用赖着要去,你去能干好工作吗?面试官通常比你更清楚这个是事情.在比如谈恋爱,姑娘有更好的目标,不喜欢你了,决定离开你了,你再怎么挽留都是没意义的, ...

  9. 第七天 py

  10. 在node环境使用axios发送文件

    yarn add form-data (async () => { const l = console.log; const axios = require("axios") ...