C ++: how to get c # properties

Is it possible to have C #-like properties in C ++? At first I thought not, and I don't have a source that claims he has it.

But I found a header file (which seems to be written in C # at first, but is actually C ++), and in that file it appears to declare properties with the property keyword:

property Boolean AlphaToCoverageEnable
{
    Boolean get()
    {
        return alphaToCoverageEnable;
    }

    void set(Boolean value)
    {
        alphaToCoverageEnable = value;
    }
}

      

I tried to use this in eclipse but didn't like it ... Is there a way to do this? The header I need to include or do something with compiling it?

The file I found: https://dl.dropbox.com/u/847423/D3D10Structs.h

+3


source to share


4 answers


You're looking at C ++ / CLI, which is Microsoft's C ++ extension that adds .NET support.

C ++ doesn't support properties, but you can create things that act like properties by abusing operator overloads.



For example see http://msdn.microsoft.com/en-us/magazine/cc534994.aspx

+2


source


Simply put, C ++ (the language) does not support C # style properties. Properties are implemented with two functions: setter and getter. Even C # properties are translated at the CLI level in a setter / getter function.



It looks like the Microsoft C ++ / CLI C ++ extensions support C # style properties, but standard ANSI C ++ doesn't.

+1


source


This is C ++ / CLI, not standard C ++. C ++ / CLI supports properties as it is designed to work seamlessly with .NET concepts.

You can directly specify in the class declaration public value struct BlendDescription

- value struct

defines the C ++ / CLI value type ( struct

in C #).

0


source


I used the code from this CodeGuru project: http://www.codeproject.com/Articles/118921/C-Properties

Using this, I created a "properties.h" file containing this:

#define PROPERTY(t,n)  __declspec( property 
( put = property__set_##n, get = property__get_##n ) ) t n;\
typedef t property__tmp_type_##n
#define READONLY_PROPERTY(t,n) __declspec( property (get = property__get_##n) ) t n;\
typedef t property__tmp_type_##n
#define WRITEONLY_PROPERTY(t,n) __declspec( property (put = property__set_##n) ) t n;\
typedef t property__tmp_type_##n
#define GET(n) property__tmp_type_##n property__get_##n() 
#define SET(n) void property__set_##n(const property__tmp_type_##n& value)   

      

with an example property:

PROPERTY(LatLonAltTuple, LatLonAlt);
GET(LatLonAlt) { return m_LatLonAlt; }
SET(LatLonAlt) { m_LatLonAlt = value; }

      

0


source







All Articles