1

I have an API I need to loosely emulate as I'm extending it (so no changing conventions), and one of the things I'm having to do is offer setter/getter like access to some private members with a singly name method implemented once for setting and once for getting through signature change.

It looks something like this

class MyClass{
    float m_t1;
    float m_t2;

public:
    const float t1();
    const float t2();

    void t1(float);
    void t2(float);
};


MyClass::MyClass(){
    m_t1 = 0.0;
    m_t2 = 0.0;
}


const float MyClass::t1(){
    return m_t1;
}

const float MyClass::t2(){
    return m_t2;
}

void MyClass::t1(float t_val){
    m_t1 = t_val;
}

void MyClass::t2(float t_val){
    m_t2 = t_val;
}

My question would be, is there a name to this? Maybe as a pattern or as something else.

Though not my first choice if I was doing things from scratch I don't necessarily mind it, but any pointers for me to be able to read about it (caveats, advice, do's and dont's in case it's actually a thing) would be appreciated, just in case it's a pattern with nasty surprises somewhere down the line (in cases a bit more complex than just throwing two floats around).

If anybody wants to share their take on it of course that'd be a bonus, but I'll be happy with just links to further reading.

Kasra
  • 3,045
  • 3
  • 17
  • 34
  • Like a function that "toggles?" call it and its true call again and its false. – Evan Carslake Feb 12 '15 at 04:36
  • I believe most people just call that overloading. Some languages have this built in, for example C# calls this properties. – Matthew Feb 12 '15 at 04:36
  • Personally, I'm not a huge fan of the overload style, but it's a personal preference. – Ed S. Feb 12 '15 at 04:40
  • @EvanCarslake it's not a toggle. Functionally it's setter/getter (private members with access gated by public methods), just semantically different because without the explicit distinction in the method name common to setter/getter: where people usually go setMyvar() / getMyvar(). I was simply wondering about whether there are fringe cases with the pattern (especially if templated). –  Feb 12 '15 at 05:01

1 Answers1

3

I believe that is Objective-C setter/getter style. For example, see this and this.

There is no surprise with overloading methods as long as you know C++ overloading well. If not, then I suggest to read more about overloading.

Personally, since I come from a Java background, I like having setVariable and getVariable names. But again, it is completely personal.

Community
  • 1
  • 1
Kasra
  • 3,045
  • 3
  • 17
  • 34
  • I'm familiar enough with overloading in C++, so I have no real issues implementing it or understanding what is going on; it's just occasionally there are fringe cases only language lawyers seem to be able to dig up and I found asking pre-emptively has spared me pain in the past. I struggled searching for this specific case though as all the keywords are basic and hugely popular for a number of other things (i.e. overload setter/getter). Thanks. –  Feb 12 '15 at 04:56