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 does C++ define member functions as const types?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "C++ how to define the member function as the const type", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "C++ how to define member functions as const types"!

Con.2: by default, member functions are defined as const types

Reason (reason)

As long as the observable state of the object is not modified, the member function should be defined as a const type. This is a clearer expression of the design intent, which leads to better readability, makes it easier for the compiler to catch more errors, and sometimes leads to more optimization opportunities.

Example, bad (negative example)

Class Point {

Int x, y

Public:

Int getx () {return x;} / / BAD, should be const as it doesn't modify the object's state

/ /...

}

Void f (const Point& pt)

{

Int x = pt.getx (); / / ERROR, doesn't compile because getx was not marked const

} Note (note)

Passing a pointer or reference to a non-constant is not necessarily bad, but it should only happen if a call to an object is assumed to modify the value of the object. The reader of the code must assume that the function that takes the original T* or T & argument will modify the referenced object. If (modification, translator's note) does not happen now, it may happen later and no recompilation is required.

Note (Note)

Some code / libraries provide functions that define T* parameters but do not modify T. This is a problem for people who update their code to make it work now with CAccord Category +. You can

Update the library to be const-correct; preferred long-term solution

Update the library to a version that correctly defines the const properties; a long-term solution is preferred.

"cast away const"; best avoided

Use constant type conversions; it's best to avoid this.

Provide a wrapper function

Provide a wrapper function

Example (example):

Void f (int* p); / / old code: F () does not modify `* p`

Void f (const int* p) {f (const_cast (p));} / / wrapper

Note that packaging is a "patch" scheme that can only be used if the declaration of f () cannot be changed. For example, when a function makes part of a library unmodifiable.

Note (Note)

Member functions of type const can modify the value of an object through the mutable object or with the help of pointer members. A common use is to maintain a cache to avoid duplicate calculations. For example, the Data class in the following code represents a string for simple reuse caching (memory).

Class Date {

Public:

/ /...

Const string& string_ref () const

{

If (string_val = "") compute_string_rep ()

Return string_val

}

/ /...

Private:

Void compute_string_rep () const; / / compute string representation and place it in string_val

Mutable string string_val

/ /...

}

Another way to explain this is that constant properties are not transferable. It is possible for a const member function to modify the value of a mutable member, or to access the object value through a non-const pointer. It is the work of this class to ensure that this modification is consistent with the semantics (invariants) it provides to the user.

Enforcement (implementation recommendations)

Flag a member function that is not marked const, but that does not perform a non-const operation on any member variable.

If a function is not defined as a const type, mark it if it does not perform a non-constant operation on any member variable.

At this point, I believe you have a deeper understanding of "how C++ defines the member function as the const type". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report