Re: accessor member functions and const
From: B. v Ingen Schenau (bart_at_ingen.ddns.info)
Date: 02/04/05
- Previous message: worzel: "Re: Value of C-C++ over Java in the general cases"
- In reply to: Faheem Mitha: "Re: 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: Fri, 04 Feb 2005 09:16:36 +0100
Faheem Mitha wrote:
<snip>
> By the way, the original motivation for doing all this was because I
> had changed my code recently, and one of my class objects was being
> mysteriously modified, and it was not clear where. It turned out that
> this was somehting of the form
>
> model_being_modified = some_other_model
>
> This is the implicitly defined copy assigment operator at work. I had
> forgotten that C++ implicitly defines such things. Any suggestions
> about good methods of how to set up access control for this? One idea
> I read would be to declare the copy assignment operator (and copy
> constructor) private, and then declare functions that need to access
> this functionality as friend functions. Would that work?
That depends on the circumstances. If assignment is only allowed in a
very small set of functions and those functions are closely connected
with the Model class, then the approach of friend-functions could work.
Otherwise, you are giving yourself a maintenance headache and a false
sense of security.
If the assignment that you mentioned above was done in a suspicious
context, like as the test in an if-statement (you wanted ==, but you
forgot to type one =-sign), then it would be a good idea to crank up
the warning level of your compiler as high as it will go. Many
compilers can warn about such apparent mistakes.
Another option is to use a lint tool, like PCLint, FlexeLint or splint
(if that last one supports C++, I am not sure). These tools are
especially designed to find as many dubious constructs in your code as
possible.
>
> Thanks for the very helpful advice. Faheem.
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/
- Previous message: worzel: "Re: Value of C-C++ over Java in the general cases"
- In reply to: Faheem Mitha: "Re: 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 ]