我有这门课:
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/