A Multiple Getter/Setter Implementation

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes