我怎样才能让这个函数像左值一样?

发布于 2024-08-26 11:50:11 字数 468 浏览 9 评论 0原文

为什么我不能使用函数 ColPeekHeight() 作为左值?

class View
{
    public:
        int ColPeekHeight(){ return _colPeekFaceUpHeight; }
        void ColPeekHeight( int i ) { _colPeekFaceUpHeight = i; }
    private:
        int _colPeekFaceUpHeight;
};

...

{
    if( v.ColPeekHeight() > 0.04*_heightTable )
        v.ColPeekHeight()-=peek;
}

编译器在 v.ColPeekHeight()-=peek 处进行抱怨。如何使 ColPeekHeight() 成为左值?

Why can't I use the function ColPeekHeight() as an l-value?

class View
{
    public:
        int ColPeekHeight(){ return _colPeekFaceUpHeight; }
        void ColPeekHeight( int i ) { _colPeekFaceUpHeight = i; }
    private:
        int _colPeekFaceUpHeight;
};

...

{
    if( v.ColPeekHeight() > 0.04*_heightTable )
        v.ColPeekHeight()-=peek;
}

The compiler complains at v.ColPeekHeight()-=peek. How can I make ColPeekHeight() an l-value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2024-09-02 11:50:12

通过引用返回成员变量:

int& ColPeekHeight(){ return _colPeekFaceUpHeight; }

为了使您的类成为一个好的类,请定义该函数的 const 版本:

const int& ColPeekHeight() const { return _colPeekFaceUpHeight; }

当我声明函数时
两个 const

当您想要将对象传递给函数时,您不希望它修改您的对象。举个例子:

struct myclass
{
    int x;
    int& return_x() { return x; }
    const int& return_x() const { return x; }
};
void fun(const myclass& obj);

int main()
{
    myclass o;
    o.return_x() = 5;
    fun(o);
}
void fun(const myclass& obj)
{
    obj.return_x() = 5; // compile-error, a const object can't be modified
    std::cout << obj.return_x(); // OK, No one is trying to modify obj
}

如果您将对象传递给函数,那么您可能不想一直更改它们。因此,为了防止这种更改,您可以声明成员函数的 const 版本。不一定每个成员函数都有两个版本!这取决于函数本身,它本质上是否修改函数:)

第一个 const 表示返回值是常量。第二个const表示成员函数return_x不会更改对象(只读)。

Return the member variable by reference:

int& ColPeekHeight(){ return _colPeekFaceUpHeight; }

To make your class a good one, define a const version of the function:

const int& ColPeekHeight() const { return _colPeekFaceUpHeight; }

when I declare the function with the
two consts

When you want to pass an object into a function that you don't expect it to modify your object. Take this example:

struct myclass
{
    int x;
    int& return_x() { return x; }
    const int& return_x() const { return x; }
};
void fun(const myclass& obj);

int main()
{
    myclass o;
    o.return_x() = 5;
    fun(o);
}
void fun(const myclass& obj)
{
    obj.return_x() = 5; // compile-error, a const object can't be modified
    std::cout << obj.return_x(); // OK, No one is trying to modify obj
}

If you pass your objects to functions, then you might not want to change them actually all the time. So, to guard your self against this kind of change, you declare const version of your member functions. It doesn't have to be that every member function has two versions! It depends on the function it self, is it modifying function by nature :)

The first const says that the returned value is constant. The second const says that the member function return_x doesn't change the object(read only).

红颜悴 2024-09-02 11:50:12

它可以重写为:

class View
{
    public:
        int  GetColPeekHeight() const  { return _colPeekFaceUpHeight; }
        void SetColPeekHeight( int i ) { _colPeekFaceUpHeight = i; }
    private:
        int _colPeekFaceUpHeight;
};

...

{
    cph = v.GetColPeekHeight();
    if ( cph > 0.04 * _heightTable )
        v.SetColPeekHeight( cph - peek );
}

It can be rewritten like:

class View
{
    public:
        int  GetColPeekHeight() const  { return _colPeekFaceUpHeight; }
        void SetColPeekHeight( int i ) { _colPeekFaceUpHeight = i; }
    private:
        int _colPeekFaceUpHeight;
};

...

{
    cph = v.GetColPeekHeight();
    if ( cph > 0.04 * _heightTable )
        v.SetColPeekHeight( cph - peek );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文