Defining Properties in C++ similar to C# ???

Hello
I found this code and it works in Visual C++ 6.0 and also in Microsoft Visual Studio 2005. But as per "The C++ Programming Language" there is no keyword like
property
.

Can any one please put more light on internals of this snippet?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

class Sample 
{
private:
        int m_nLength; 

public: 
        __declspec ( property ( put = SetFunction, get = GetFunction ) ) 
                int Lenght ;
        void SetFunction (int nInputData )
        { 
                if ( nInputData < 0 ) 
                        m_nLength = 0 ; 
                else 
                        m_nLength = nInputData ; 
        }

        int GetFunction( ) const
        { 
                return m_nLength ; 
        } 
};


void main( )
{ 
        Sample obj ;
        obj.Lenght = 100 ;
        cout << obj.Lenght << " this is valu of a.length"<< endl; 
} 



Thanks
GM
You can create your own property class if you don't want Microsoft non-standard stuff
You *can* create your own, but it's hideous. Here's one way to do it, which gives some insight into what MS is doing. It would probably be better to define the get and set functions in the template, not the constructor, however.

If anyone knows how to do this without passing *this to the constructor of the Property object, I'd love to see it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
template<class _Prop_t,
         class _ObjClass_t>
class Property
{
    typedef _Prop_t (_ObjClass_t::* _pmGet_t)() const;
    typedef void (_ObjClass_t::* _pmSet_t)(_Prop_t);

    _ObjClass_t& m_objInstance;
    _pmGet_t     m_pmGet;
    _pmSet_t     m_pmSet;

public:
    Property(_ObjClass_t& objInstance, _pmGet_t pmGet, _pmSet_t pmSet)
    :  m_objInstance(objInstance), m_pmGet(pmGet), m_pmSet(pmSet)
    {}
    operator _Prop_t() { return (m_objInstance.*m_pmGet)(); }
    void operator =(_Prop_t value) { (m_objInstance.*m_pmSet)(value); }
};


class Sample 
{
private:
        int m_length; 

public: 
        int GetLength( ) const
        { 
                return m_length ; 
        } 

        void SetLength (int nInputData )
        { 
                if ( nInputData < 0 ) 
                        m_length = 0 ; 
                else 
                        m_length = nInputData ; 
        }

        Property<int, Sample> Length;

        Sample()
            : m_length(0), Length(*this, &Sample::GetLength, &Sample::SetLength)
        {}
};



int main(int argc, char* argv[])
{
    Sample obj ;
    obj.Length = 100 ;
    std::cout << obj.Length << " this is valu of a.length"<< std::endl; 

    return 0;
}
closed account (z05DSL3A)
I have not read it but I clocked this the other day:

Implementing Properties In C++
By Emad Barsoum
http://www.codeproject.com/KB/cpp/cppproperties.aspx


Acualy this is the one I saw the other day:
C++ implementation of the C# Property and Indexer with Accessor-Modifiers
By jeff00seattle
http://www.codeproject.com/KB/cpp/cpp_property_indexer.aspx
Last edited on
Topic archived. No new replies allowed.