c++ - 在 C++ 中使用私有(private)方法的属性

标签 c++ class visual-c++ properties private

我有这门课:

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 时,使用了私有(private)方法。

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

最佳答案

How can i make a property that uses functions which CAN'T be used outside the class?

恐怕只能使用另一种语言。标准 C++ 没有这样的属性糖(很遗憾),虽然有一些 hack,但都不是很漂亮,包括 Microsoft 的。有关私有(private)方法与 Microsoft declspec(property) 扩展不兼容的特定问题的更多讨论,请参见此处: http://blog.aaronballman.com/2011/11/an-almost-useful-language-extension/

关于c++ - 在 C++ 中使用私有(private)方法的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15598894/

相关文章:

c++ - 无法设置 CWinApp 上的 SetTimer 函数

c++ - 如何计算传递给可变参数宏的宏参数的数量?

c++ - 在类对象段错误中使用 boost::interprocess ,为什么?

c++ - 当构造函数抛出异常时删除运算符段错误

C# 使用 FindWindowEx 按名称和序号获取子句柄

c++ - 如何纠正多态性

IOS:获取类类型的通用方法

unit-testing - 在 Visual Studio 中使用 Catch2 进行单元测试的最佳实践

c++ - 损坏的堆栈;用字符串进行位操作;微软VC++

c++ - 使用 C++ 的 Windows 线程