目录

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. VMware vCenter Server 6.5安装

    实验环境: 数据中心操作系统 :  Windows server 2008 R2(建议配置8G内存) 数据中心安装包版本: VMware vCenter Server 6.5 数据中心数据库:     ...

  2. Jquery EasyUI Combotree只能选择叶子节点且叶子节点有多选框

    Jquery EasyUI Combotree只能选择叶子节点且叶子节点有多选框 Jquery EasyUI Combotree单选框,Jquery EasyUI Combotree只能选择叶子节点 ...

  3. unity 5.3 以后加载场景

    记录下官方建议的加载场景的方法: StartCoroutine(LoadYourAsyncScene()); IEnumerator LoadYourAsyncScene() { // The App ...

  4. LeetCode - 498. Diagonal Traverse

    Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...

  5. C# WinForm窗体控件GroupBox修改边框颜色控件

    C# WinForm窗体控件GroupBox修改边框颜色控件 1.新建组件这里可以自定义一个GroupBox控件起名为GroupBoxEx 2.增加一个BoderColor属性 private Col ...

  6. [Golang] kafka集群搭建和golang版生产者和消费者

    一.kafka集群搭建 至于kafka是什么我都不多做介绍了,网上写的已经非常详尽了. 1. 下载zookeeper  https://zookeeper.apache.org/releases.ht ...

  7. Python __all__变量用法

    Python中一个py文件就是一个模块,“__all__”变量是一个特殊的变量,可以在py文件中,也可以在包的__init__.py中出现. 1.在普通模块中使用时,表示一个模块中允许哪些属性可以被导 ...

  8. mock---前端搭建模拟服务

    在做前端开发接口的时候,往往接口都是从后端来的,这让前端很苦恼,但是有了 MockServer ,前端也可以搭建API服务了. server-mock是什么? 是一款nodejs的应用,用于搭建web ...

  9. js版的in_array的实现方法

    这是一个JS版的判断数组内的元素的方法. var arr = ['a','b','c']; console.log(in_array('b',arr)); // true function in_ar ...

  10. Nest.js 守卫

    Docs: https://docs.nestjs.com/guards 当调用者具有足够的权限时,特定路由才可用 // app.guard.ts import { CanActivate, Exec ...