Re: accessor member functions and const
From: Anthony Borla (ajborla_at_bigpond.com)
Date: 02/02/05
- Next message: David Flick: "Re: String handling in .txt file."
- Previous message: Nicolas Pavlidis: "Re: "Global" variable need declared in main()?!"
- 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 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
- Next message: David Flick: "Re: String handling in .txt file."
- Previous message: Nicolas Pavlidis: "Re: "Global" variable need declared in main()?!"
- 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
|