Re: accessor member functions and const

From: B. v Ingen Schenau (bart_at_ingen.ddns.info)
Date: 02/02/05


Date: Wed, 02 Feb 2005 20:21:00 +0100

Faheem Mitha wrote:

> 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.

It is one way of giving access. With a member-function like wval(), you
might just as well make the data-member 'v' itself public because that
would make no difference in what users of your class can do with it.

A typical approach would be like this:
class model
{
private:
  int v;
public:
  model(const int& _v):v(_v){}
  model(){}
  const int& getVal() const {return v;}
  void setVal(const int& new_v) {v = new_v;}
/* or even
  const int& val() const {return v;}
  void val(const int& new_v) {v = new_v;}
*/
};

This way, you have some control (inside setVal) over the value that gets
stored in v, so you can for example, limit the allowable range of
values.

>
> 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. 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.

Correct.

>
> b) The const after the wval() means that the function is declared
> const, so it is not allowed to modify the class object.

Not entirely correct.
A const-qualified mnember-function may not take any action that could
lead to the alteration of the object it is called for. That also means
that it is not allowed to return a non-const reference to a member of
the 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.

And what if you call wval() on an object that is truely not modifyable,
because it resides in read-only memory?
If wval() was declared as const, the compiler can not forbid such a
call, because it can not in general know if wval gives a modifyable
reference to some part of the object or if it returns a reference to
some completely unrelated variable.

>
> Thanks in advance for any replies.
>
> Faheem.
>
<snip - code>

Bart v Ingen Schenau

-- 
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/


Relevant Pages

  • Re: accessor member functions and const
    ... > reference the returned value v. ... int& val ... const int& valconst ... so one cannot modify the object via the reference. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Passing const objects as arguments to function or return types
    ... the compiler throws an error as soon as you try to declare const ... so passing handles to const is pretty useless: ... function which then can easily modify the object received ... The tracking reference is the version of the C++ reference that supports ...
    (microsoft.public.dotnet.languages.vc)
  • 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: const int value changing....
    ... > when a 'const int ' is declared, where is it been created(in RAM?) ... the compiler won't allow you to modify a const variable. ...
    (comp.lang.c)
  • Re: typedef
    ... I think you mean const on reference type is ignored. ...
    (microsoft.public.vc.language)