Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Call mutato...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 5 Topic 45883 of 47032
Post > Topic >>

Re: Call mutators or attributes directly inside class?

by Andrey Tarasevich <andreytarasevich@[EMAIL PROTECTED] > May 12, 2008 at 09:00 AM

Travis wrote:
> Is it considered good practice to call a mutator when inside the same
> class or modify the attribute directly?
> 
> So if there's a public method SetName() would it be better from
> say ::Init() to call SetName("none") or just set name="none"?
> 

It depends.

The purpose of writing accessor methods (mutators etc.) is not just the
blind 
formal replacement of direct attribute access with mutators. The purpose
of 
writing accessors is to provide interface that decouples the clients of
the 
class from the physical implementation details of the corresponding
attribute. 
The benefits of this decoupling apply to other cl***** as much as they
apply to 
the class itself. For this reason the reasonable strategy to follow is to
use 
accessor methods whenever you can, and use direct attribute access only
when you 
must.

When you are writing methods that belong to the same class as the
attribute in 
question, the choice depends on the nature of the functionality under 
consideration. If this is a very low-level functionality that is supposed
to 
have knowledge of/rely on the implementation details of the class, then
you 
might choose the access the attribute directly (for the sake of
efficiency). If 
this is a higher level functionality, you might be better off using the
accessor 
methods.

-- 
Best regards,
Andrey Tarasevich
 




 5 Posts in Topic:
Call mutators or attributes directly inside class?
Travis <travis.bowers@  2008-05-12 08:26:15 
Re: Call mutators or attributes directly inside class?
Christopher <cpisz@[EM  2008-05-12 08:41:44 
Re: Call mutators or attributes directly inside class?
Andrey Tarasevich <and  2008-05-12 09:00:18 
Re: Call mutators or attributes directly inside class?
Travis <travis.bowers@  2008-05-12 09:26:02 
Re: Call mutators or attributes directly inside class?
=?UTF-8?B?RXJpayBXaWtzdHL  2008-05-12 16:56:36 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 15:53:44 CDT 2008.