A Multiple Getter/Setter Implementation

Suppose you’ve got a user interface class with many user options that you need to set/get at the same time. Here’s one way to implement this:

class CUI{public:  CUI() : m_bOption(false), m_cOption(''), m_iOption(0), m_dOption(0.0){}  //...  void GetOptions(bool bOption, char cOption, int& riOption, double&rdOption)  {    bOption = m_bOption;    cOption = m_cOption;    riOption = m_iOption;    rdOption = m_dOption;  }  void SetOptions(bool bOption, char cOption, int const& riOption, doubleconst& rdOption)  {    m_bOption = bOption;    m_cOption = cOption;    m_iOption = riOption;    m_dOption = rdOption;  }private:  //...  bool m_bOption;  char m_cOption;  int m_iOption;  double m_dOption;};

The problem with this implementation is that when the number of options increases, you need to transfer a larger amount of data as arguments to the getter/setter functions. This is not an efficient way to make use of the program’s stack. The solution is to create an inner structure containing all the options:

#include using namespace std;class CUI{public:  struct SOptions  {    SOptions() : m_bOption(false), m_cOption(''), m_iOption(0),m_dOption(0.0) {}    bool m_bOption;    char m_cOption;    int m_iOption;    double m_dOption;  };  CUI() {}  //...  void GetOptions(SOptions& roOptions)  {    roOptions.m_bOption = m_oOptions.m_bOption;    roOptions.m_cOption = m_oOptions.m_cOption;    roOptions.m_iOption = m_oOptions.m_iOption;    roOptions.m_dOption = m_oOptions.m_dOption;  }  void SetOptions(SOptions const& roOptions)  {    m_oOptions.m_bOption = roOptions.m_bOption;    m_oOptions.m_cOption = roOptions.m_cOption;    m_oOptions.m_iOption = roOptions.m_iOption;    m_oOptions.m_dOption = roOptions.m_dOption;  }private:  //...  SOptions m_oOptions;};
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may