当前位置:网站首页>Pointer parameter passing vs reference parameter passing vs value parameter passing
Pointer parameter passing vs reference parameter passing vs value parameter passing
2022-07-05 21:35:00 【MrL_ JJ】
List of articles
Preface
Have you ever encountered such a dilemma , Calling someone else's api when , Whether it's value transfer , The reference , Transmission pointer , It's easy to use , But I'm making it myself api when , But I got stuck here. I should adopt pointer to pass parameters , Value pass parameter or reference ? This dilemma of uncertain choices .
One 、 Pointer and reference are different
The pointer : Pointer is also a variable , What is stored is the address of other variables , So a pointer accessing a variable is an indirect access . A pointer is an entity , Memory space needs to be allocated . Pointers don't have to be initialized when they are defined , And the space that points to is variable . Pointer self increment operation , The result pointer points to the next space .
quote : References are just aliases for variables , There is no need to allocate memory space . Reference access to a variable is a direct access to . References must be initialized when they are defined , And it can't change . The underlying reference is implemented through pointers ; Quote autoincrement , The result is the quoted variable value plus 1
It's also different as a parameter , The essence of passing a pointer is to pass a value , The value passed is the address of the pointer ; The essence of transmission reference is address , Passing the address of the variable , So when should I pass the pointer , When will it be transmitted and quoted ?
Two 、 For functions that do not modify the incoming parameters
1. If the data object is small , Such as built-in data types or small structures , Is passed by value ;
2. If the data object is an array , Then use the pointer ( The only choice ), And the pointer is declared as pointing const The pointer to ;
3. If the data object is a large structure , Then use const Pointer or reference , The efficiency of the procedure has been improved . This can save the time and space required by the structure ;
4. If the data object is a class object , Then use const quote ( The standard way to pass class object parameters is by reference );
3、 ... and 、 For functions that modify incoming parameters
1、 If the data is a built-in data type , Then use the pointer
2、 If the data object is an array , Only pointers can be used
3、 If the data object is a structure , Use a reference or pointer
4、 If the data is a class object , Use reference
One last word : Pointer is not type safe ( There are more references than pointers, type checking ) It's best to do type checking before using pointers , Prevent the appearance of wild pointer ;
边栏推荐
- vant 源码解析 之深层 合并对象 深拷贝
- Clion configures Visual Studio (MSVC) and JOM multi-core compilation
- vant 源码解析之 utils/index.ts 工具函数
- Realize the function of verifying whether the user has completed login when browsing the page
- 2.2 basic grammar of R language
- Get JS of the previous day (timestamp conversion)
- Teach yourself to train pytorch model to Caffe (III)
- ESP32
- Recursive query of multi-level menu data
- sql常用语法记录
猜你喜欢
張麗俊:穿透不確定性要靠四個“不變”
DBeaver同时执行多条insert into报错处理
Pytorch实战——MNIST数据集手写数字识别
CLion配置visual studio(msvc)和JOM多核编译
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
递归查询多级菜单数据
Cross end solution to improve development efficiency rapidly
Making global exception handling classes with aspect
2.2 basic grammar of R language
R language [data management]
随机推荐
Introduction of ArcGIS grid resampling method
Haas506 2.0 development tutorial - Alibaba cloud OTA - PAC firmware upgrade (only supports versions above 2.2)
Postgres establish connection and delete records
張麗俊:穿透不確定性要靠四個“不變”
【案例】元素的显示与隐藏的运用--元素遮罩
Utils/index TS tool function
Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
uni-app 蓝牙通信
Determine the best implementation of horizontal and vertical screens
Aitm 2-0003 horizontal combustion test
秋招将临 如何准备算法面试、回答算法面试题
Which securities company is better and which platform is safer for stock account opening
Influence of oscilloscope probe on signal source impedance
Teach yourself to train pytorch model to Caffe (III)
Selenium gets the verification code image in DOM
WPF gets the control in the datagridtemplatecolumn of the specified row and column in the DataGrid
Why can't Chinese software companies produce products? Abandon the Internet after 00; Open source high-performance API gateway component of station B | weekly email exclusive to VIP members of Menon w
股票开户选择哪家证券公司比较好哪家平台更安全
【日常训练--腾讯精选50】89. 格雷编码(看题解才会的)
Opérations de lecture et d'écriture pour easyexcel