Re: getters and setters

Discussion in 'C++' started by E. Robert Tisdale, Jun 21, 2005.

  1. 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.
     
    E. Robert Tisdale, Jun 21, 2005
    #1
    1. Advertising

  2. 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/ApacheDOMC 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
     
    Steven T. Hatton, Jun 21, 2005
    #2
    1. Advertising

  3. 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.
     
    Victor Bazarov, Jun 21, 2005
    #3
  4. E. Robert Tisdale

    Default User Guest

    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
     
    Default User, Jun 21, 2005
    #4
  5. 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.
     
    E. Robert Tisdale, Jun 21, 2005
    #5
  6. E. Robert Tisdale

    Default User Guest

    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
     
    Default User, Jun 21, 2005
    #6
  7. OT: Re: getters and setters

    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
     
    Steven T. Hatton, Jun 21, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Armhold
    Replies:
    1
    Views:
    1,470
    Chris Riesbeck
    Sep 2, 2004
  2. Getters and Setters

    , Jun 9, 2006, in forum: Java
    Replies:
    7
    Views:
    704
    Eddy Young
    Jun 10, 2006
  3. Daniel Fetchinson

    setters and getters in python 2.6 and 3.0

    Daniel Fetchinson, Nov 29, 2007, in forum: Python
    Replies:
    2
    Views:
    595
    Daniel Fetchinson
    Nov 29, 2007
  4. TechieInsights
    Replies:
    5
    Views:
    366
    josh logan
    Feb 12, 2009
  5. Lachlan Hunt
    Replies:
    2
    Views:
    171
    Lachlan Hunt
    Mar 28, 2005
Loading...

Share This Page