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

What are the C++ constants?

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

Share

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

C++ constants have what, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this to learn, I hope you can gain something.

1. constant parameter

When a formal parameter has a top-level const or a bottom-level const, it can be passed to a constant object or a non-constant object. Here we mainly consider the case where the formal parameter has no const and the actual parameter has const. In fact, it is also very simple here. Just remember one thing: the limitation of the bottom-level const is OK.

2. constant return value

It's also very simple here, skip it.

3. constant member function

Const in constant functions is used to modify *this, and its form is as follows:

int f() const{```}

And then this gets interesting.

The default type of *this is type *const this, this pointer has a top-level const, but there is no low-level const, due to the limitation of low-level const, the argument with the low-level const cannot be copied to the default version of *this, that is, the reference or pointer of the constant object cannot call the default version of *this member function.

It's not over yet...

Just now we have made it clear that arguments with low-level const cannot initialize the default version of *this, but can objects with top-level const initialize the default version of *this and then call the function?

No, no.

//define a simple class studentclass student{public: string name; int number;public : student() :name("lili"), number(0){ }//constructor string Name(){return name;}//nonconstant member function int Number() const{return number;}//constant member function};//define a constant student object const student s1;s1.Name();//error s1.Number();//correct

In fact, when we call s1.Name(), the following initialization takes place:

student *const this=&s1;

This is equivalent to the following process:

const student *s1;student *const this=s1;

Obviously s1 has an underlying const and this doesn't, initialization fails.

Similarly, if an argument is int *const p, then when initializing this, it translates to const int *const p, with an underlying const, initialization failure.

Constant objects, pointers to constant objects, or references to constant objects can only call constant member functions.

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

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