Re: accessor member functions and const
From: B. v Ingen Schenau (bart_at_ingen.ddns.info)
Date: 02/02/05
- Next message: Gary Labowitz: "Re: is a constant a literal?"
- Previous message: gooch: "Re: Value of C-C++ over Java in the general cases"
- In reply to: Faheem Mitha: "accessor member functions and const"
- Next in thread: Faheem Mitha: "Re: accessor member functions and const"
- Reply: Faheem Mitha: "Re: accessor member functions and const"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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/
- Next message: Gary Labowitz: "Re: is a constant a literal?"
- Previous message: gooch: "Re: Value of C-C++ over Java in the general cases"
- In reply to: Faheem Mitha: "accessor member functions and const"
- Next in thread: Faheem Mitha: "Re: accessor member functions and const"
- Reply: Faheem Mitha: "Re: accessor member functions and const"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|