c++中使用私有方法的属性

Properties using private methods in c++

本文关键字:有方法 属性 c++      更新时间:2023-10-16

我有这样一个类:

class Size
{
    private:
        int size_x;
        int size_y;
        int GetX( void );
        int GetY( void );
        void PutX( int x );
        void PutY( int y );
    public:
        _declspec ( property ( get = GetX , put = PutX ) ) int X;
        _declspec ( property ( get = GetY , put = PutY ) ) int Y;
};

这段代码不能工作,因为当我试图访问X或Y时,使用了私有方法。

我怎么能使一个属性,使用不能在类外使用的函数?

如何创建一个使用不能使用的函数的属性课外?

恐怕只能用另一种语言。标准c++没有这样的属性糖(可悲的是),虽然有一些hack,但没有一个是非常漂亮的,包括微软的。关于私有方法与Microsoft declspec(property)扩展不兼容的具体问题,请参阅这里的更多讨论:http://blog.aaronballman.com/2011/11/an-almost-useful-language-extension/