Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use const in C++ member function

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use const in C++ member functions". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

modification

First, we need to understand that there are two ways to call functions in C++: passing values and passing references.

value is passed

When values are passed, a copy of the input parameter is created when the function is called, and the operation in the function does not modify the original value, so there is no need to use const to modify the input parameter in this way, because it only operates on the copied temporary object.

address transfer

The operation in the function when passing the address actually modifies the original value directly, so we can use const to modify the input parameter here.

const modification into reference

When const modifier function parameter means that the parameter can not be modified, this is the best understanding, such as a function function is copied, then the source file in the parameter will be modified with const.

void A::show(const int *b) { cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report