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

  • Home
  • SEARCH
  • 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 194031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:33:38+00:00 2026-05-11T16:33:38+00:00

I was reading this morning the book The Pragmatic Programmer Chapter 3 on Basic

  • 0

I was reading this morning the book The Pragmatic Programmer Chapter 3 on Basic Tools every programmer should have and they mentioned Code Generation Tools.
They mentioned one Perl script for C++ programs which helped automate the process of implementing the get/set() member functions for private data members.

Does anyone know about such a script and where to find it? I’ve been unable to come up with the right google keywords to find it.

  • 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-11T16:33:38+00:00Added an answer on May 11, 2026 at 4:33 pm

    Although it doesn’t directly answer your question, you may find that generated code is actually unnecessary for managing properties in C++. The following template code will allow you to declare and use properties conveniently:

    // Declare your class containing a few properties
    class my_class {
    public:
        property<int> x;
        property<string> y;
        ...
    };
    
    ...
    
    my_class obj;
    cout << obj.x();          // Get
    obj.y("Hello, world!");   // Set
    

    Here is the code:

    // Utility template to choose the 2nd type if the 1st is void
    template <typename T, typename U>
    struct replace_void {
        typedef T type;
    };
    
    template <typename T>
    struct replace_void<void, T> {
        typedef T type;
    };
    
    // Getter/setter template
    template <typename T, typename D = void>
    class property {
        typedef typename replace_void<D, property>::type derived_type;
    
        derived_type& derived() { return static_cast<derived_type&>(*this); }
    
    public:
        property() {}   // May be safer to omit the default ctor
        explicit property(T const& v) : _v(v) {}
        property(property const& p) : _v(p._v) {}
        property& operator=(property const& p) { _v = p._v; return *this; }
    
        T operator()() const { return _v; }                 // Getter
        void operator()(T const& v) { derived().check(v); _v = v; }   // Setter
    
    protected:
        // Default no-op check (derive to override)
        void check(T const& v) const { (void)v; //avoid unused variable warning}
    
    private:
        T _v;
    };
    

    check() is a function that tests whether the value being assigned is valid. You can override it in a subclass:

    class nonnegative_int : public property<int, nonnegative_int> {
    public:
        // Have to redeclare all relevant ctors unfortunately :(
        nonnegative_int(int v) : property<int, nonnegative_int>(v) {}
    
        void check(int const& v) const {
            if (v < 0) {
                throw "Yikes! A negative integer!";
            }
        }
    };
    

    There you have it — all of the advantages of externally-generated getter/setter functions, with none of the mess! 🙂

    You could choose to have check() return a bool indicating validity instead of throwing an exception. And you could in principle add a similar method, access(), for catching read references to the property.

    EDIT: As Mr. Fooz notes in the comments, the class author can later change the implementation without modifying the logical structure of the class (e.g. by replacing the property<int> x member with a pair of x() methods), although binary compatibility is lost so users will need to recompile their client code whenever such a change is made. This ability to painlessly incorporate future changes is actually the main reason people use getter/setter functions instead of public members in the first place.

    Performance note: Because we are using the CRTP to achieve “compile-time polymorphism”, there is no virtual-call overhead for providing your own check() in a subclass, and you need not declare it virtual.

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

Sidebar

Ask A Question

Stats

  • Questions 151k
  • Answers 151k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can write a decorator bean class that will wrap… May 12, 2026 at 9:52 am
  • Editorial Team
    Editorial Team added an answer As a first step, I would definitely recommend reading Scott… May 12, 2026 at 9:52 am
  • Editorial Team
    Editorial Team added an answer I fixed this thing by going with images instead of… May 12, 2026 at 9:52 am

Related Questions

This morning, I was reading Steve Yegge's: When Polymorphism Fails , when I came
This morning I ran into an issue with returning back a text string as
Good morning, afternoon, evening or night (depending on your timezone). This is just a
I am out of ideas as to why my app has suddenly stopped working

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.