Re: accessor member functions and const

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 02/02/05


Date: Wed, 02 Feb 2005 12:17:03 GMT


"Faheem Mitha" <faheem@email.unc.edu> wrote in message
news:slrnd00sdm.4je.faheem@Chrestomanci.home.earth...
> Dear People,
>
> Please consider the following code. The functions val and
> wval are, respectively, read-only and write access member
> functions of the class model.
>
> I have two simple questions.
>
> 1) Does my implementation look reasonable? In the both
> the read-only and write access case, I want to be able to
> reference the returned value v.
>

Here is a slight variation of your class which includes:

* Stylistic changes; class names tend to be capitalised,
   while trailing underscores are 'safer' in that they are
   less likely to conflict with system-reserved names

* Member function overloading - the difference in 'constness'
   is enough for the compiler to distinguish between them

* Extra constructor, ensuring that any such object contains
   data that is always initialised

class Model
{
private:
  int v;

public:
  Model(const int& v_) : v(v_) {}
  Model() : v(0) {}

  const int& val() const { return v; }
  int& val() { return v; }

  // Added for illustration purposes
  const int& val(const int& r) const { return r; }
  int& val(int& r) const { return r; }
};

int main()
{
  Model mod;

  const int vr = mod.val(); // const version selected
  mod.val() = 1; // non-const version selected

  return 0;
}

> 2) Using
>
> int& wval() const {return v;}
>
> gives
>
> invalid initialization of reference of type 'int&' from
> expression of type 'const int'
>
> I'm not sure why this is a problem.
>

Add the following to your class:

    int& val(int& r) const {return r;}

and you should notice that it *does* compile. This should tell you that as
long as a 'const' function does not do anything to allow alteration of the
object's state, it is allowable.

>
> My understanding is that
>
> a) The const int& guarantees that the function returns a constant
> reference, so one cannot modify the object via the reference.
>
> b) The const after the wval() means that the function is declared
> const, so it is not allowed to modify the class object.
>
> However, even if the function itself is not allowed to modify the
> class object, I don't see why one cannot modify it via the non-const
> reference that the function returns.
>

It has to do more than merely not modify the object's state - it has to
'promise' not to do anything that will allow the object's state to possibly
be altered. Hence it *cannot* return a non-const reference to a data member.

I hope this helps.

Anthony Borla



Relevant Pages

  • Re: accessor member functions and const
    ... I want to be able to reference the returned ... const int& getValconst ... so one cannot modify the object via the reference. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Using const qualifier
    ... But it returns a pointer through which you could modify the list. ... Do you use "const" to imply "I will not change this, ... const int *operate; ... constraint: typeofis typeof; ...
    (comp.lang.c)
  • Re: help: class compile error
    ... > defines a global int i. so I can use ... > class CTest ... reference, unless they somehow _change_ the arguments. ... take its argument by a _const_ reference (unless it intends to change ...
    (comp.lang.cpp)
  • accessor member functions and const
    ... I want to be able to reference the returned ... expression of type 'const int' ... so one cannot modify the object via the reference. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Read-only, as opposed to const member
    ... int GetNumberOfHolesconst; ... > or less efficient than returning a const reference. ...
    (comp.lang.cpp)