Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use const in C++ member function

Shulou Source: shulou.com Published: 2022-06-01 06:08:23 10月03日 Update

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

Tags: Functions parameters members objects code codes identical variables actual that is copies pointers data methods knowledge examples input output and then two. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple NVidia macOS MySQL