使用公共方法c ++设置类的私有属性

Nic*_*sui 1 c++ methods properties private class

像这样的东西:

class someclass
{
public:
    someclass();
    ~someclass();

    long   Set(int x, int y);

private:
        int _x;
        int _y;
};

long   Set(int x, int y)
{
   _x = x;
   _y = y;
}
Run Code Online (Sandbox Code Playgroud)

但是如果你只是写这样的东西,那么在Set()函数中就无法识别_x.那么如何使用自己的方法设置类的私有属性呢?非常感谢.

Rod*_*uis 6

你有一个范围问题.这应该工作:

long someclass::Set(int x, int y)
{
   _x = x;
   _y = y;
}
Run Code Online (Sandbox Code Playgroud)

  • @BoBTFish:whew:p (2认同)