43

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?

Alcott
  • 17,905
  • 32
  • 116
  • 173

5 Answers5

42

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;}
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Sir, your explanation is pretty clear and right. IMO, `foo` in `class A` opens a door to modifying what `_px` pointing to, which,I think, makes `foo` a non-const member function, right? – Alcott Jan 09 '12 at 13:25
  • @Alcott: `const` on a member function only protects members of the object itself, not any further levels of indirection. So within the function `_px` is effectively `int * const`; the pointer itself can't be modified, but its target can be. – Mike Seymour Jan 09 '12 at 13:47
  • Actually you _can_ return a non `const` reference from a `const` member function like so: `struct O { int* a; int& getA() const { return *a; }` – bobobobo Oct 30 '21 at 20:06
5

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
5

It does not open a door to modifying _px but rather what _px points to. It's up to you to decide whether you want to allow this or not.

For example, an iterator::operator-> would return a non-const pointer and const_iterator::operator-> would return a const pointer. Both methods can be const themselves.

visitor
  • 1,781
  • 10
  • 7
3

Yes, for your case it can. However, it's generally advised to not to do this, because it allows changing constant objects:

void f(const A& a) 
{
  *(a.foo()) = 42; // damn!
}
sbi
  • 219,715
  • 46
  • 258
  • 445
0

Yes, for example see the std::streambuf pointers:

protected:
   char* pbase() const;
   char* pptr() const;
   char* epptr() const;

http://en.cppreference.com/w/cpp/io/basic_streambuf/pptr

John Duffy
  • 164
  • 4
  • 10