Jump to content
  • Advertisement
Sign in to follow this  
soconne

__declspec property feature in Visual C++

This topic is 3635 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I just discovered __declspec property and how it can alleviate all the headaches of not having class property accessors in C++, or having to create bloated class objects simply for wanting an integer value property. http://msdn.microsoft.com/en-us/library/yhfk0thd(VS.80).aspx Now I realize this is a Microsoft extension for C++, but is anything similar to this supported by other compilers, especially gcc, mingw, ect...? Anything that's cross-platform?

Share this post


Link to post
Share on other sites
Advertisement
Personally, I would just stick with using set/get functions rather than touch that ugly property syntax ;D

Each to their own though, so here's my attempt at a portable property class:
template<class T, class P, const T& (P::*get)(), void (P::*put)(const T&)>
class Property
{
public:
Property( P& parent ) : parent(parent) {} P& parent;
operator const T&() const { return (parent.*get)(); }
Property<T,P,get,put>& operator=( const T& rhs ) { (parent.*put)(rhs); return *this; }
private:
Property( const Property<T,P,get,put>& );
Property<T,P,get,put>& operator=( const Property<T,P,get,put>& );
};
It's very clunky itself; you've got to initialize the property using a reference to the object that set/get will be called on, and no operators can be used (besides casting to T, and assigning from T). At least this isn't compiler specific though.

And a test/example:
class Test
{
public:
Test() : prop(*this), i(0) {}
const int& GetInt() { return i; }
void SetInt( const int& in ) { i = in; }
Property<int,Test,&Test::GetInt,&Test::SetInt> prop;
private:
int i;
};
void PropertyTest()
{
Test obj;
int i = obj.prop;
ASSERT( i == 0 );
obj.prop = 10;
i = obj.prop;
ASSERT( i == 10 );
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!