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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:21:30+00:00 2026-05-22T00:21:30+00:00

In Visual Studio, there is __declspec(property) which creates properties similar to C#. Borland C++

  • 0

In Visual Studio, there is __declspec(property) which creates properties similar to C#. Borland C++ offers the __property keyword with the exact same functionality. In the C++0x, there is mention of a implicit keyword that could be expanded to implement the same functionality. But it didn’t make it into the spec.

I am looking for a portable and relatively clean method of declaring syntactically sugared properties that will compile in the latest compilers for Windows, OSX and Linux. I am not concerned with compiler compatibility, just one compiler per platform.

I am not looking for alternatives to properties that require parenthesis to get or set the property, such as overloaded methods separating the getters and setters.

Here is an ideal usage which compiles in Visual Studio 2010:

#define _property(_type, _name, _get, _put) __declspec(property(get=_get, put=_put)) _type _name
#define _property_readonly(_type, _name, _get) __declspec(property(get=_get)) _type _name

class Window
{
public:
    _property_readonly(void*, Handle, GetHandle);
    _property(bool, Visible, GetVisible, SetVisible);

    void* GetHandle();
    bool GetVisible();
    void SetVisible(bool);
}

void main()
{
    Window MainWindow;
    if (!MainWindow.Visible)
        MainWindow.Visible = true;
}
  • 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-22T00:21:31+00:00Added an answer on May 22, 2026 at 12:21 am

    This is something similar to what you are asking and is (I hope) standard C++…

    #include <iostream>
    
    template<typename C, typename T, T (C::*getter)(), void (C::*setter)(const T&)>
    struct Property
    {
        C *instance;
    
        Property(C *instance)
            : instance(instance)
        {
        }
    
        operator T () const
        {
            return (instance->*getter)();
        }
    
        Property& operator=(const T& value)
        {
            (instance->*setter)(value);
            return *this;
        }
    
        template<typename C2, typename T2,
                 T2 (C2::*getter2)(), void (C2::*setter2)(const T2&)>
        Property& operator=(const Property<C2, T2, getter2, setter2>& other)
        {
            return *this = (other.instance->*getter2)();
        }
    
        Property& operator=(const Property& other)
        {
            return *this = (other.instance->*getter)();
        }
    };
    
    //////////////////////////////////////////////////////////////////////////
    
    struct Foo
    {
        int x_, y_;
    
        void setX(const int& x) { x_ = x; std::cout << "x new value is " << x << "\n"; }
        int getX() { std::cout << "reading x_\n"; return x_; }
    
        void setY(const int& y) { y_ = y; std::cout << "y new value is " << y << "\n"; }
        int getY() { std::cout << "reading y_\n"; return y_; }
    
        Property<Foo, int, &Foo::getX, &Foo::setX> x;
        Property<Foo, int, &Foo::getY, &Foo::setY> y;
    
        Foo(int x0, int y0)
            : x_(x0), y_(y0), x(this), y(this)
        {
        }
    };
    
    int square(int x)
    {
        return x*x;
    }
    
    int main(int argc, const char *argv[])
    {
        Foo foo(10, 20);
        Foo foo2(100, 200);
        int x = foo.x; std::cout << x << "\n";
        int y = foo.y; std::cout << y << "\n";
        foo.x = 42; std::cout << "assigned!\n";
        x = foo.x; std::cout << x << "\n";
        std::cout << "same instance prop/prop assign!\n";
        foo.x = foo.y;
        std::cout << "different instances prop/prop assign\n";
        foo.x = foo2.x;
        std::cout << "calling a function accepting an int parameter\n";
        std::cout << "square(" << foo.x << ") = " <<  square(foo.x) << "\n";
        return 0;
    }
    

    As you can see from main the usage is transparent as long as you are assigning values of type T (here int) or implicitly convertible to T to properties and as long you are converting them back to T values on reading.

    Behavior will be different however if you for example pass foo.x to a template function because the type of foo.x is not int but Property<Foo, int, ...> instead.

    You can also have problems with non-template functions… calling a function accepting a T value will work fine, however a T& parameter is for example going to be a problem because basically the function is asking a variable to access directly using the address. For the same reason you cannot pass of course the address of a property to a function accepting a T* parameter.

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

Sidebar

Related Questions

in visual studio 2008 there is a sub-menu called create instance which is resides
Is there a g++ equivalent to Visual Studio's __declspec(novtable) argument? Basically, in a pure
In Visual Studio there are some file properties such as Build Action, Custom Tool,
In Visual Studio there is a tool called TaskList, which searches the code for
In Visual Studio 2008 there is a folder browser dialog that looks like this
Possible Duplicate: Adding a guideline to the editor in Visual Studio Is there a
in visual studio 6 is there a way to Find All References. Don't see
visual studio 2008 allows c# code to be segregated into regions is there an
Is there a Visual Studio plugin that spellchecks strings and comments? Duplicate of Visual
Is there an official Visual Studio test project template for NUnit? If not, what

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.