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 8369589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:41:16+00:00 2026-06-09T13:41:16+00:00

I wonder how to hide a real property field (not make it private or

  • 0

I wonder how to hide a real property field (not make it private or public but force to use setters and getters) and provide him with simple setter and getter. So I wonder how to create api like:

private:
    Property( int my_a);
public:
    Property( int my_b);
...
{
 set_my_a(1);
 cout << get_my_a() << endl;
 // my_a = 13; // will cause compiler error
...

How to create such thing via Boost preprocessor?

  • 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-06-09T13:41:17+00:00Added an answer on June 9, 2026 at 1:41 pm

    Do you really need to use boost preprocessor?
    you have a solution without boost below:

    // property.h    
    #include <stdio.h>
    
    #define property(type) struct : public Property <type>  
    
    template <typename T>
    class Property 
    {
    protected:
      T value;
    public:
      virtual T get() { 
        return value; 
      }
      virtual void set(T new_value) { 
        value = new_value; 
      }
    };
    

    usage example:

    // test.cpp
    #include "property.h"
    
    class Test {
    public:
      property(int) {} a;
    
      property(int)  {
        int get() { 
          return value * 10; 
        } 
      } b;
    
      property(int) {
        void set(int x) {
          value = x * 200;
        } 
      } c;
    
      property(int) {
        int get() { 
          return value * 3000; 
        } 
        void set(int x) {
          value = x * 443;
        } 
      } d;
    };
    
    main()
    {
      Test t;
    
      printf("i\ta\tb\tc\td\t\n");
      for (int i=0; i<10; i++) { 
        t.a.set(i);
        t.b.set(i);
        t.c.set(i);
        t.d.set(i);
        printf("%i\t%i\t%i\t%i\t%i\n", i, t.a.get(), t.b.get(), t.c.get(), t.d.get());
      }
    }
    

    The wikipedia solution in http://en.wikipedia.org/wiki/Property_(programming)#C.2B.2B is good but needs a minimal modification to become useful, because without the protected statement you cant write your own getters and setters.

    #include <iostream>
    
    template <typename T> 
    class property {
    protected:
      T value;
    public:
      T & operator = (const T &i) {
        ::std::cout << i << ::std::endl;
        return value = i;
      }
      operator T const & () const {
        return value;
      }
    };
    
    struct Bar {
      property <bool> alpha;
      struct :public property <int> {
        int & operator = (const int &i) {
          ::std::cout << "new setter " << i << ::std::endl;
          return value = i;
        }
      } bravo;
    };
    
    main() 
    {
      Bar b;
      b.alpha = false;
      b.bravo = (unsigned int) 1;
    }
    

    You can change a little more if you want:

    #include <iostream>
    #define SETTER(type) public: type& operator=(const type new_value)
    #define GETTER(type) public: operator type const & () const
    
    template <typename T> 
    class Property {
    protected:
      T value;
    public:
      T & operator = (const T &i) {
        ::std::cout << i << ::std::endl;
        return value = i;
      }
      template <typename T2> T2 & operator = (const T2 &i) {
        ::std::cout << "T2: " << i << ::std::endl;
        T2 &guard = value;
        throw guard; // Never reached.
      }
      operator T const & () const {
        return value;
      }
    };
    
    struct Bar {
      Property <bool> alpha;
      struct:Property <int> {
        SETTER(int) {
          value = new_value * 1000;
          ::std::cout << "new method " << new_value << ::std::endl;
          return value;
        }
        GETTER(int) {
          return value/1000;
        }
      } bravo;
    };
    
    main() 
    {
      Bar b;
      b.alpha = false;
      b.bravo = (unsigned int) 1;
      ::std::cout << b.bravo << ::std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder, using Eclipse's PyDev plugin , how come documentation does not always show
I wonder if there is a way to hide html tags when echoing a
I wonder if it's somehow possible to use the stop() function (I normally use
I wonder - what is the best way to supply contextual (i.e. not related
I have this code that I use to POST without reloading the page, but
I'm currently building up a small Real Time Strategy 2D engine. And I wonder
I wonder how can i hide the attached panel in a way that only
I wonder if there is a way to hide some document library lists that
Well I think this is kinda dumb but, I wonder if there's any way
I wonder why this isn't working as expected: $(function() { $(.door1-trigger).click(function() { $(.door).hide(); //

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.