返回 &const 或 *const? (c++)
我有自己的班级“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两者都不好:
const SomeObject &const
格式不正确。您不能对引用进行常量限定。 (当然,您可以限定引用类型。)const SomeObject *const
不必要地冗长。函数调用表达式o.GetSomeObject()
是一个右值表达式,并且只有类类型的右值可以是 const 限定的。您也可以直接说const SomeObject*
。 (const SomeObject *const
实际上可能会导致模板实例化问题,尽管此类问题很少见。)至于选择通过指针返回还是通过引用返回,这取决于您如何使用返回值价值。两者在不同情况下都有意义。无论如何,您希望返回指向 const 对象的引用或指针,而不是 const 引用或 const 指针:
通常,返回引用更好。
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 expressiono.GetSomeObject()
is an rvalue expression and only class-type rvalues can be const-qualified. You may as well just sayconst 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:
Usually, returning a reference is preferable.
编写
& const
是毫无意义的。引用始终是恒定的。省略那里的 const 关键字。如果您想返回一个常量非空对象,那么使用引用会更好。如果您想返回对象或空指针,请改用指针。
另请参阅什么时候应该使用引用,什么时候应该使用指针?
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?