Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 532601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:25:19+00:00 2026-05-13T09:25:19+00:00

Unless I am thoroughly mistaken, the getter/setter pattern is a common pattern used for

  • 0

Unless I am thoroughly mistaken, the getter/setter pattern is a common pattern used for two things:

  1. To make a private variable so that it can be used, but never modified, by only providing a getVariable method (or, more rarely, only modifiable, by only providing a setVariable method).
  2. To make sure that, in the future, if you happen to have a problem to which a good solution would be simply to treat the variable before it goes in and/or out of the class, you can treat the variable by using an actual implementation on the getter and setter methods instead of simply returning or setting the values. That way, the change doesn’t propagate to the rest of the code.

Question #1: Am I missing any use of accessors or are any of my assumptions incorrect? I’m not sure if I am correct on those.

Question #2: Are there any sort of template goodness that can keep me from having to write the accessors for my member variables? I didn’t find any.

Question #3: Would the following class template be a good way of implementing a getter without having to actually write the accesor?

template <class T>
struct TemplateParameterIndirection // This hack works for MinGW's GCC 4.4.1, dunno others
{
    typedef T Type;
};

template <typename T,class Owner>
class Getter
{
public:
    friend class TemplateParameterIndirection<Owner>::Type; // Befriends template parameter

    template <typename ... Args>
    Getter(Args args) : value(args ...) {} // Uses C++0x

    T get() { return value; }

protected:
    T value;
};

class Window
{
public:
    Getter<uint32_t,Window> width;
    Getter<uint32_t,Window> height;

    void resize(uint32_t width,uint32_t height)
    {
        // do actual window resizing logic

        width.value = width; // access permitted: Getter befriends Window
        height.value = height; // same here
    }
};

void someExternalFunction()
{
    Window win;

    win.resize(640,480); // Ok: public method

    // This works: Getter::get() is public
    std::cout << "Current window size: " << win.width.get() << 'x' << win.height.get() << ".\n";

    // This doesn't work: Getter::value is private
    win.width.value = 640;
    win.height.value = 480;
}

It looks fair to me, and I could even reimplement the get logic by using some other partial template specialization trickery. The same can be applied to some sort of Setter or even GetterSetter class templates.

What are your thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:25:20+00:00Added an answer on May 13, 2026 at 9:25 am

    Whilst the solution is neat from implementation point of view, architectually, it’s only halfway there. The point of the Getter/Setter pattern is to give the clas control over it’s data and to decrease coupling (i.e. other class knowing how data is stored). This solution achieves the former but not quite the latter.

    In fact the other class now has to know two things – the name of the variable and the method on the getter (i.e. .get()) instead of one – e.g. getWidth(). This causes increased coupling.

    Having said all that, this is splitting proverbial architectural hairs. It doesn’t matter all that much at the end of the day.

    EDIT OK, now for shits and giggles, here is a version of the getter using operators, so you don’t have to do .value or .get()

    template <class T>
    struct TemplateParameterIndirection // This hack works for MinGW's GCC 4.4.1, dunno others
    {
        typedef T Type;
    };
    
    template <typename T,class Owner>
    class Getter
    {
    public:
        friend TemplateParameterIndirection<Owner>::Type; // Befriends template parameter
    
        operator T()
        {
            return value;
        }
    
    protected:
        T value;
    
        T& operator=( T other )
        {
           value = other;
           return value;  
        }
    
    
    };
    
    class Window
    {
    public:
        Getter<int,Window> _width;
        Getter<int,Window> _height;
    
        void resize(int width,int height)
        {
            // do actual window resizing logic
            _width = width; //using the operator
            _height = height; //using the operator
        }
    };
    
    void someExternalFunction()
    {
        Window win;
    
        win.resize(640,480); // Ok: public method
        int w2 = win._width; //using the operator
        //win._height = 480; //KABOOM
    }
    

    EDIT Fixed hardcoded assignment operator. This should work reasonably well if the type itself has an assignment operator. By default structs have those so for simple ones it should work out of the box.

    For more complex classes you will need to implement an assignment operator which is fair enough. With RVO and Copy On Write optimizations, this should be reasonably efficient at run time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.