Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can const member function return a non-const pointer to a data member?

Tags:

c++

constants

Code goes first:

class A {     public:         ...         int *foo() const         {             return _px;         }     private:         int *_px; } 

The member function foo returns a non-const pointer to private member _px, which, I think, opens a door to modifying member _px, right?

Is foo a const member function? Should I add a const in front of the return type?

UPDATE

What a const-member-function should guarantee is that, it cannot change any data-member, right?

In my case, function foo doesn't open a door to modifying class As data-member _px, but a door to modifying what _px pointing to, So my question is, does this violate what a const-function should guarantee?

like image 870
Alcott Avatar asked Jan 09 '12 13:01

Alcott


People also ask

Can a const member function call a non-const function?

Const member functions in C++ It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object. Non-const functions can be called by non-const objects only.

Can a const member function modify a const data member?

const member functions Declaring a member function with the const keyword specifies that the function is a "read-only" function that doesn't modify the object for which it's called. A constant member function can't modify any non-static data members or call any member functions that aren't constant.

Can a const reference be bound to a non-const object?

No. A reference is simply an alias for an existing object. const is enforced by the compiler; it simply checks that you don't attempt to modify the object through the reference r .

What will happen if a const object calls a non-const member function?

If the function is non-constant, then the function is allowed to change values of the object on which it is being called. So the compiler doesn't allow to create this chance and prevent you to call a non-constant function on a constant object, as constant object means you cannot change anything of it anymore.


2 Answers

A const member function can only return a const pointer or reference to a member.

However, your example isn't returning a pointer to a member; it's returning a copy of a member that happens to be a pointer. That is allowed in a const member function (even if the pointer happens to point to another member).

This would not be allowed (note that it's now returning a reference):

int *& foo() const {return _px;} 

but this would (returning a const reference):

int * const & foo() const {return _px;} 
like image 189
Mike Seymour Avatar answered Sep 26 '22 19:09

Mike Seymour


int *_px becomes int *const _px inside a const member function this implies that the pointer cannot be reseated but the data pointed to is still modifyable. Further your function returns a copy of the pointer so it does not matter anyways.

like image 44
Alok Save Avatar answered Sep 26 '22 19:09

Alok Save