3

In project I'm working on (external sources) I've encountered a class...

class SignalGroup
{
public:
  SignalGroup() 
  : amber_(2)
  , minRed_(0)
  , minGreen_(0)
  , ...
  {}

  unsigned int GetAmber() {
    return amber_;
  }
  unsigned int GetMinRed() {
    return minRed_;
  }
  unsigned int GetMinGreen() {
    return minGreen_;
  }

  ...

  unsigned int & Amber() {
    return amber_;
  }
  unsigned int & MinRed() {
    return minRed_;
  }
  unsigned int & MinGreen() {
    return minGreen_;
  }
  ...



private:
  unsigned int amber_;
  unsigned int minRed_;
  unsigned int minGreen_;
  ...
};

The class is used in conjunction with CDialog::DoDataExchange() and the reference getters are used to exchange data with fields in a dialog box. ( DDX_Text(pDX, IDC_TXTAMBER, sg->Amber()); ) The value getters (not reference) are used everywhere else.

Does this approach make some sense I don't understand, or is it missing something to be what it should be (getters private, the CDialog override class being a friend of SignalGroup) or did the author simply commit a faux pas, as mentioned in the answer to a related question?

Community
  • 1
  • 1
SF.
  • 13,549
  • 14
  • 71
  • 107
  • The related question sums up the issues with providing references to class members pretty well. It is valid C++ code, so it is really up to you if you want to design it that way. – lcs Jun 30 '16 at 15:38
  • 2
    Generally getters and setters are the anti-pattern. The way you class is working there really is no reason not to make it all public access for everything since you do nothing in the getters. – NathanOliver Jun 30 '16 at 15:38
  • @NathanOliver: Since nobody except the Dialog is allowed to modify the values, I'm considering leaving the value getters in, getting rid of the reference getters, and making the DataExchange class a friend. That way encapsulation is retained everywhere outside the two objects that are tightly bound anyway. – SF. Jun 30 '16 at 15:52
  • errr... scratch that previous comment. The whole class gets the axe, I don't need it anyway, I was just trying to understand what was done here. – SF. Jun 30 '16 at 15:53
  • Sounds good. I just wanted to give you a heads up. – NathanOliver Jun 30 '16 at 15:55

0 Answers0