C++ Const pointer declaration

僤鯓⒐⒋嵵緔 提交于 2019-12-18 14:12:55

问题


I am reviewing some code and I ran across some code I am unfamiliar with. After some searching I could not come up of any example of why this is done or the benefit of this declaration.

myClass const * const myPtr = myClass->getPointer();

Is this a declaration of a const pointer or something entirely different?


回答1:


It means "myPtr is a const pointer to a const myClass". It means that you can neither modify what the pointer is pointing at through this pointer nor can you make the pointer point somewhere else after it's initialised (by the return value of myClass->getPointer()). So yes, you're basically right, with the addition that it also points to a const object (as far as you know; it could really be non-const underneath).

Remember that const applies to the item to its left (or if there is no item to its left, the item to its right). The first const makes the myClass const (where you can't modify what the pointer points at) and the second const makes the * const (where you can't modify the pointer itself).




回答2:


In some cases, you may see it written like this:

const myClass * const myPtr = myClass->getPointer(); 

The two are equivalent, namely a constant pointer to a constant object. I prefer the syntax as you've shown it because it is easier to understand for newbies when read right-to-left:

Object * const obj;        // read right-to-left:  const pointer to Object
Object const * obj;        // read right-to-left:  pointer to const Object
Object const * const obj;  // read right-to-left:  const pointer to const Object 



回答3:


The rule is that the const keyword applies to the "word" on the left of it, unless it's the first word on the line, in which case it applies to the "word" on the right of it. So: myClass const * const: myClass <- const, pointer <- const, const pointer to const myClass.

For some reason, in most code (at least the one I've seen), this would be declared as const myClass * const. Why I have no clue, because now you have to take the exception to the "const applies to word on the left, unless it's first word on the line" into account. I.e.: writing lines they way is done in the code you are reviewing is a lot clearer.




回答4:


This declares a constant pointer to a constant value, returned by calling the member function of a dereference pointer myClass




回答5:


Seth is on the money. There's some more background you might find useful on Duramecho as well.




回答6:


The first const is a valid use while the second const is superfluous IMO.

When you use

myClass const * const myPtr = myClass->getPointer();

You cannot change the object that myPtr points to.
You cannot change where myPtr points to either.

myPtr->SomeMember = SomeValue; // Not allowed. That's OK.
                               // The state of myClass is protected.

myPtr = nullptr; // That's not allowed either.
                 // I don't think it is necessary to disallow this.

You should be able to use

myClass const * myPtr = myClass->getPointer();

without any harm.



来源:https://stackoverflow.com/questions/9779540/c-const-pointer-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!