Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: getters and setters

Reply
Thread Tools

Re: getters and setters

 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      06-20-2005
kelvSYC wrote:

> I'm familiar with get and set function paradigms from Java
> but what's the recommended design for such in C++?
> Should it be like so:
>
> foo& getFoo(void);


It isn't usually a good idea
to return a [non-const] reference to an object
which is part of another object
unless that other object is a *container*
for objects of type Foo.

const
Foo& getFoo(void) const;

would be better and it can be applied to const objects.

> void setFoo(Foo& f);


Try to avoid void functions.
Return a reference to the modified object
so that this function can be used in an expression.

> or like so:
>
> Foo& foo(void);
> const
> Foo& foo(void) const;
>
> or some combination of the above?


Yes. The get and set prefixes are superfluous.

class Bar {
public:
class Foo {
private:
// representation
int I;
public:
Foo(int i = 0): I(i) { }
};
private:
// representation
Foo F;
public:
const
Foo& foo(void) const { return F; }
Foo& foo(void) { return F; }
};

If you are going to return a non-const reference
to an object which is *not* an element of a container,
you should retain control over the definition
of that object so that you can change it later
without breaking any code that uses it.
In the above example, nesting the definition of Foo
inside the definition of Bar guarantees that
the Bar library developer retains control
over the definition of Foo.
 
Reply With Quote
 
 
 
 
Steven T. Hatton
Guest
Posts: n/a
 
      06-21-2005
E. Robert Tisdale wrote:

> kelvSYC wrote:
>

....
> Try to avoid void functions.
> Return a reference to the modified object
> so that this function can be used in an expression.


That seems like reasonable advice. There's also another option in some
cases.

ParentWidget* pw(new ParentWidget);
ChildWidget* cw(pw->addChildWidget(new ChildWidget));

Or a real world example:
http://xml.apache.org/xerces-c/Apach...BindingL3.html
virtual DOMNode* insertBefore(DOMNode* newChild, DOMNode* refChild) = 0;
virtual DOMNode* replaceChild(DOMNode* newChild, DOMNode* oldChild) = 0;
virtual DOMNode* removeChild(DOMNode* oldChild) = 0;
virtual DOMNode* appendChild(DOMNode* newChild) = 0;

>> or like so:
>>
>> Foo& foo(void);
>> const
>> Foo& foo(void) const;
>>
>> or some combination of the above?

>
> Yes. The get and set prefixes are superfluous.


So I'm the only one, eh?

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-21-2005
Steven T. Hatton wrote:
> E. Robert Tisdale wrote:
>
>> [...]
>> Yes. The get and set prefixes are superfluous.

>
> So I'm the only one, eh?



No, of course not. You're in good company.


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      06-21-2005


Victor Bazarov wrote:
> Steven T. Hatton wrote:
> > E. Robert Tisdale wrote:
> >
> >> [...]
> >> Yes. The get and set prefixes are superfluous.

> >
> > So I'm the only one, eh?

>
>
> No, of course not. You're in good company.



Our coding standard at work mandates the prefixes (actually "Get" and
"Set").



Brian

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      06-21-2005
Default User wrote:

> Victor Bazarov wrote:
>
>>Steven T. Hatton wrote:
>>
>>>E. Robert Tisdale wrote:
>>>
>>>>[...]
>>>>Yes. The get and set prefixes are superfluous.
>>>
>>>So I'm the only one, eh?

>>
>>No, of course not. You're in good company.

>
> Our coding standard at work mandates the prefixes
> (actually "Get" and "Set").


My condolences.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      06-21-2005


E. Robert Tisdale wrote:
> Default User wrote:


> > Our coding standard at work mandates the prefixes
> > (actually "Get" and "Set").

>
> My condolences.



Get lost, Trollsdale. Basically, if you're against something, I'll
assume it's good.



Brian

 
Reply With Quote
 
Steven T. Hatton
Guest
Posts: n/a
 
      06-21-2005
Default User wrote:

>
>
> E. Robert Tisdale wrote:
>> Default User wrote:

>
>> > Our coding standard at work mandates the prefixes
>> > (actually "Get" and "Set").

>>
>> My condolences.

>
>
> Get lost, Trollsdale. Basically, if you're against something, I'll
> assume it's good.


Personal attacks are off topic in c.l.c++. Please restrict such comments to
private communications.

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read Only attributes, auto properties and getters and setters TechieInsights Python 5 02-12-2009 06:11 PM
setters and getters in python 2.6 and 3.0 Daniel Fetchinson Python 2 11-29-2007 09:36 PM
Getters and Setters Scott.R.Lemke@gmail.com Java 7 06-10-2006 11:01 AM
Getters and Setters For Object Properties in JS 1.5 and ECMAScript Lachlan Hunt Javascript 2 03-28-2005 01:48 PM
XMLEncoder/XMLDecoder and mutable arguments to getters/setters George Armhold Java 1 09-02-2004 05:43 PM



Advertisments