如何编写 Const 和 Mutable 重载代码?

发布于 2024-08-18 05:22:13 字数 793 浏览 5 评论 0原文

我的代码中似乎经常出现这种模式,除了参数/返回的常量性之外,两个函数执行相同的任务。

int& myClass::getData()
{
        return data;
}

// called for const objects 
const int& myData::getData() const
{
        return data;
}

这冒犯了我的 DRY 观念。对于单行代码来说这不是问题,但是随着 getData() 变大,就会出现明显的重复。

我知道为什么我需要这两种方法,但觉得应该有更好的方法来实现它。是否有一个模板技巧可以提供帮助,或者我应该有一种方法来调用另一种方法来根据需要来回转换常量?

添加:作为一个更真实的示例,下面是典型 STL vector::at() 实现的示例:

const_reference at(size_type _Off) const
    {   // subscript nonmutable sequence with checking
    if (size() <= _Off)
        _Xran();
    return (*(begin() + _Off));
    }

reference at(size_type _Off)
    {   // subscript mutable sequence with checking
    if (size() <= _Off)
        _Xran();
    return (*(begin() + _Off));
    }

I seem to have this pattern occuring pretty often in my code, with two functions performing the same task apart from the constness of their parameters/returns.

int& myClass::getData()
{
        return data;
}

// called for const objects 
const int& myData::getData() const
{
        return data;
}

This offends my sense of DRY. It's not a problem for a one-liner, but as getData() gets bigger, there's obvious duplication.

I know WHY I need both methods, but feel there should be a better way to implement it. Is there a template trick that can help, or should I have one method which calls the other casting the constness back and forth as required?

ADDED: As a more real-world example, here's a sample of typical STL vector::at() implementation:

const_reference at(size_type _Off) const
    {   // subscript nonmutable sequence with checking
    if (size() <= _Off)
        _Xran();
    return (*(begin() + _Off));
    }

reference at(size_type _Off)
    {   // subscript mutable sequence with checking
    if (size() <= _Off)
        _Xran();
    return (*(begin() + _Off));
    }

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

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

发布评论

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

评论(3

冷心人i 2024-08-25 05:22:13

使用以下技巧(我最初从 Scott Meyers 的书 Effective C++ 中得到):

int& myClass::getData()
{
    // This is safe because we know from out here
    // that the return value isn't really const
    return const_cast<int&>(const_cast<const myClass&>(*this).getData());
}

const int& myData::getData() const
{
    return data;
}

显然,对于像这样的短函数,您可能会发现复制代码更容易,但是当您具有更长的函数(例如 vector::at 或者它可能会发生很多变化。

Use the following trick (which I originally got from Scott Meyers' book Effective C++):

int& myClass::getData()
{
    // This is safe because we know from out here
    // that the return value isn't really const
    return const_cast<int&>(const_cast<const myClass&>(*this).getData());
}

const int& myData::getData() const
{
    return data;
}

Obviously for a short function like this, you may find it easier just to duplicate code, but this idiom is useful when you have a longer function (like vector<T>::at or it is subject to lots of changes.

埋葬我深情 2024-08-25 05:22:13

提供如下功能

int& myClass::getData()
{
    return data;
}

可能是错误的 - 您不妨公开“数据”。第二个应该写成:

int myData::getData() const
{
   return data;
}

Providing a function like:

int& myClass::getData()
{
    return data;
}

is probably wrong - you might as well make 'data' public. And the second should be written as:

int myData::getData() const
{
   return data;
}
怎会甘心 2024-08-25 05:22:13

您引用的标准库函数重复代码的原因是标准库函数需要尽可能高性能。在那里调用另一个函数(正如其他人正确建议的那样)可能会搞砸。但您的代码可能受到不太严格的性能限制,因此您应该尽可能使用通用代码。

The reason the Standard Library functions you quote duplicate the code is that Standard Library functions need to be as performant as possible. Putting a call to another function in there (as others have correctly suggested doing) might screw that. But your code is probably under much less stringent performance constraints, so you should use common code, where possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文