返回 &const 或 *const? (c++)

发布于 2024-10-09 08:14:12 字数 923 浏览 2 评论 0原文

我有自己的班级“SomeObject”,有一些成员。

现在,我有另一个类“WorkingClass”包含此对象作为私有成员。

我的问题是:我想为“SomeObject”创建一个 Getter,但我不希望任何人修改它。

1 或 2 哪种方式更好?

class WorkingClass
{
private:
    SomeObject sObj;

public:
    //... 1)
    const SomeObject &const GetSomeObject()
    {
        return mousePosition;
    }

    //... 2)
    const SomeObject *const GetSomeObject()
    {
        return &mouseMovement;
    }
}

我知道你总是可以抛弃 const,但是,我只是想让我的代码干净且故障安全

编辑:

然后我还有一个进一步的问题。当我有一个智能指针成员并在类中经常使用它,然后突然希望某人有权读取某些值但仅此而已,这会是一个很好的解决方案还是又很冗长?

class X
{
private:
    boost::shared_ptr<SomeObject> sob

public:
    const const & GetSomeObject()
    {
        return *sob.get();
    }
}

返回“const boost::shared_ptr<...> GetX()”怎么样?它可能不是真正必要的,但仍然不是无用的,因为编译器在这种情况下会禁止 GetX().reset(..) ,并且如果没有 const boost::... 声明,将允许这种无用的操作。还是我错了?

i have my own class "SomeObject" with a few members.

now, i have another class "WorkingClass" containg this object as privat member.

My Question is: i want to create a Getter for the "SomeObject", but i don't want anyone to modify it.

which way is better, 1 or 2?

class WorkingClass
{
private:
    SomeObject sObj;

public:
    //... 1)
    const SomeObject &const GetSomeObject()
    {
        return mousePosition;
    }

    //... 2)
    const SomeObject *const GetSomeObject()
    {
        return &mouseMovement;
    }
}

i know you can always cast away const, but still, i'm just trying to get my code clean and fail-safe

EDIT:

then i have a further question. when i have a smart-pointer member and use it a lot inside the class, and then suddenly want someone to have acces to read some values but nothing more, would this be a good solution or is that verbose again?

class X
{
private:
    boost::shared_ptr<SomeObject> sob

public:
    const const & GetSomeObject()
    {
        return *sob.get();
    }
}

and how about returning a "const boost::shared_ptr<...> GetX()" ? it may not be really neccessary, but still not useless, as the compiler would forbid GetX().reset(..) in such a case, and without the const boost::... declaration this useless operation would be permitted. or am i wrong?

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

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

发布评论

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

评论(2

绮筵 2024-10-16 08:14:12

两者都不好:

  • const SomeObject &const 格式不正确。您不能对引用进行常量限定。 (当然,您可以限定引用类型。)

  • const SomeObject *const 不必要地冗长。函数调用表达式 o.GetSomeObject() 是一个右值表达式,并且只有类类型的右值可以是 const 限定的。您也可以直接说 const SomeObject*。 (const SomeObject *const 实际上可能会导致模板实例化问题,尽管此类问题很少见。)

至于选择通过指针返回还是通过引用返回,这取决于您如何使用返回值价值。两者在不同情况下都有意义。无论如何,您希望返回指向 const 对象的引用或指针,而不是 const 引用或 const 指针:

const SomeObject& GetSomeObject() { ... }
const SomeObject* GetSomeObject() { ... }

通常,返回引用更好。

Neither is good:

  • const SomeObject &const is ill-formed. You cannot const-qualify a reference. (You can, of course, qualify the referent type.)

  • const SomeObject *const is unnecessarily verbose. A function call expression o.GetSomeObject() is an rvalue expression and only class-type rvalues can be const-qualified. You may as well just say const SomeObject*. (const SomeObject *const can actually lead to issues with template instantiation, though such issues are rare.)

As for whether you choose to return by pointer or by reference, it depends on how you are using the return value. Both can make sense in different circumstances. Regardless, you want to return a reference or pointer to a const object, not a const reference or a const pointer:

const SomeObject& GetSomeObject() { ... }
const SomeObject* GetSomeObject() { ... }

Usually, returning a reference is preferable.

同展鸳鸯锦 2024-10-16 08:14:12

编写 & const毫无意义的。引用始终是恒定的。省略那里的 const 关键字。

如果您想返回一个常量非空对象,那么使用引用会更好。如果您想返回对象或空指针,请改用指针。

另请参阅什么时候应该使用引用,什么时候应该使用指针?

可以时使用引用,必要时使用指针。

当您不需要“重新定位”时,引用通常比指针更受青睐。这通常意味着引用在类的公共接口中最有用。引用通常出现在对象的外表上,而指针则出现在内部。

上述情况的例外是函数的参数或返回值需要“哨兵”引用 - 不引用对象的引用。通常最好的方法是返回/获取指针,并赋予 NULL 指针特殊的意义(引用应始终为对象别名,而不是取消引用的 NULL 指针)。

Writing & const is pointless. References are always constant. Omit the const keyword there.

Using a reference is better if you want to return a constant non-null object. If you want to return either an object or a null pointer then use a pointer instead.

See also When should I use references, and when should I use pointers?

Use references when you can, and pointers when you have to.

References are usually preferred over pointers whenever you don't need "reseating". This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.

The exception to the above is where a function's parameter or return value needs a "sentinel" reference — a reference that does not refer to an object. This is usually best done by returning/taking a pointer, and giving the NULL pointer this special significance (references should always alias objects, not a dereferenced NULL pointer).

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