首页 > 编程语言 >C++ 值,指针,引用的讨论

C++ 值,指针,引用的讨论

时间:2022-10-20 15:13:01浏览次数:70  
标签:use reference object C++ 引用 pass pointer 指针

源自 stackoverflow 论坛,很有意义

第一个问题,引用传递和按值传递的场合

There are four main cases where you should use pass-by-reference over pass-by-value:

  1. If you are calling a function that needs to modify its arguments, use pass-by-reference or pass-by-pointer. Otherwise, you’ll get a copy of the argument.
  2. If you're calling a function that needs to take a large object as a parameter, pass it by const reference to avoid making an unnecessary copy of that object and taking a large efficiency hit.
  3. If you're writing a copy or move constructor which by definition must take a reference, use pass by reference.
  4. If you're writing a function that wants to operate on a polymorphic class, use pass by reference or pass by pointer to avoid slicing.

来源:Where should I prefer pass-by-reference or pass-by-value?

第二个问题,地址运算符(&)和引用运算符(&)的区别 代码解释,
int v = 5;

cout << v << endl; // prints 5
cout << &v << endl; // prints address of v

int* p;
p = &v; // stores address of v into p (p is a pointer to int)

int& r = v;

cout << r << endl; // prints 5

r = 6;

cout << r << endl; // prints 6
cout << v << endl; // prints 6 too because r is a reference to v

问题中更有意思的是对 *this 的解释,

Firstly, this is a pointer. The * dereferences the pointer, meaning return *this; returns the object, not a pointer to it.

Secondly, Test& is returning a reference to a Test instance. In your case, it is a reference to the object. To make it return a pointer, it should be Test*.

来源:Address-of operator (&) vs reference operator(&)

附 C++ 各个阶段学习的书籍推荐,来源论坛大牛的回答

C++ 并发编程实战感觉包含挺多工作上需要的知识点,比如,线程库、原子库、C++ 内存模型、锁和互斥锁,以及设计和调试多线程应用程序的问题,可以看看

标签:use,reference,object,C++,引用,pass,pointer,指针
From: https://www.cnblogs.com/strive-sun/p/16809942.html

相关文章

  • c++ list插入
    list::insert()用于在列表的任何位置插入元素。用法:insert(pos_iter,ele_num,ele)参数:此函数接受三个参数:pos_iter:在容器中插入新元素的位置。ele_num:要插入的......
  • Maven项目不同jar包相同类名的引用问题
    本文简单记录下一个小问题问题描述:在一个Maven项目中,引用了两个jar包,其中两个jar包中,都含有个相同类(包名也相同),这个时候代码里使用该类,出现引用失败的问题如下图所示,......
  • 【多线程那些事儿】如何使用C++写一个线程安全的单例模式?
    如何写一个线程安全的单例模式?单例模式的简单实现单例模式大概是流传最为广泛的设计模式之一了。一份简单的实现代码大概是下面这个样子的:classsingleton{public: s......
  • C++ 函数重载解析策略
    参考《C++PrimerPlus》(第6版)中文版,StephenPrata著,张海龙袁国忠译,人民邮电出版社。C++使用重载解析策略来决定为函数调用使用哪一个函数定义。重载解析过程大致分为如......
  • c++ 是歌姬吧
    为什么不能给bitset加点重载让它当高精用?为什么不能给bitset加点重载让它当高精用?为什么不能给bitset加点重载让它当高精用?为什么不能给bitset加点重载让它当高精......
  • 【番外篇】Rust环境搭建+基础开发入门+Rust与.NET6、C++的基础运算性能比较
    前言:突然想打算把Rust作为将来自己主要的副编程语言。当然,主语言还是C#,毕竟.NET平台这么强大,写起来就是爽。缘起:之前打算一些新的产品或者新的要开发的东西,由于没有历史包......
  • 实验三 数组、指针与现代C++标准库
    实验任务5info.hpp1#include<iostream>2#include<string>3#include<algorithm>4#include<vector>5usingnamespacestd;67classInfo{8......
  • C++课程设计《最短路径》
    C++课程设计《最短路径》课程设计《最短路径》课程设计题目:最短路径实验设计目的与要求:2.1目的:1)熟练应用C++的基本知识、技能。通过本课程设计,总结C++中抽象数据......
  • 为 C++程序添加 C 接口
    为C++程序添加C语言接口总共有两个要注意的点,如下要在接口定义中添加extern"C"的声明进行链接时需要使用C++的标准库原始的C++程序animal.h#ifndef_AN......
  • 实验3 数组,指针与现代C++标准库
    task5//Info.hpp#include<iostream>#include<math.h>#include<string>usingnamespacestd;classInfo{public:Info(stringa,stringb,stringc,......