C ++ const成员函数返回一个指针(C++ const member function returns a pointer)

编程入门 行业动态 更新时间:2024-10-25 14:26:02
C ++ const成员函数返回一个指针(C++ const member function returns a pointer)

即使它返回的(指针)数据成员可以在方法外部使用以更改其引用的对象,是否将方法标记为常量被认为是不好的做法?

看看bone()方法。

class SceneNode { public: SceneNode(const char *nodeName, SceneNode *parent); ~SceneNode(); void setBone(Bone *bone); Bone* bone() const { return _bone; } }; private: Bone *_bone; };

我这样问是因为它对我来说似乎有点不可思议,因为如果_bone不是一个指针,而是一个对象(当然,除非它返回一个const引用或副本,否则该方法不能是const)。

Is it considered bad practice to mark a method as const even though the (pointer) data member it returns could be used "outside" the method to change its referenced object?

Take a look at the bone() method.

class SceneNode { public: SceneNode(const char *nodeName, SceneNode *parent); ~SceneNode(); void setBone(Bone *bone); Bone* bone() const { return _bone; } }; private: Bone *_bone; };

I'm asking this because it's seems a little weird to me as the method can't be const if _bone wasn't a pointer, but an object (unless it returns a const reference or copy, of course).

最满意答案

它依赖于例如, unique_ptr::operator*()被标记为const,但不会为它返回的引用添加额外的const。 这是有道理的,因为它类似于能够修改T *const指向的内容,而不是改变指针本身。

const std::unique_ptr<int> uptr(new int); // make_unique is better *uptr = 1; // sure, I can modify what it points to uptr.reset(); // no, this modifies the pointer itself (conceptually)

另一方面,像std::string char& operator[](size_t) const; 会很奇怪,因为它里面的char应该像它们被引用的对象的一部分( string类)

const std::string s{"hello world"}; // I expect this to always be "hello world" s[0] = 'a'; // error, you're conceptually modifying the string

这取决于你决定什么在你的API中有意义,什么时候你的课堂是“道德上不变的”。 如果你有

void fun(const SceneNode& node); int main() { SceneNode n(...); Bone b; n.setBone(&b); fun(n); assert(*n.bone() == b); // is this a safe assumption? }

如果你认为断言应该总是成立,那么返回一个const Bone* 。

It depends For example, unique_ptr::operator*() is marked as const but doesn't add an additional const to the reference it returns. This makes sense because it's analogous to being able to modify what is pointed to by a T *const but not change the pointer itself.

const std::unique_ptr<int> uptr(new int); // make_unique is better *uptr = 1; // sure, I can modify what it points to uptr.reset(); // no, this modifies the pointer itself (conceptually)

On the other hand, something like std::string having char& operator[](size_t) const; would be weird because the chars inside of it should act like part of the object they are referenced by (the string class)

const std::string s{"hello world"}; // I expect this to always be "hello world" s[0] = 'a'; // error, you're conceptually modifying the string

It's up to you to decide what makes sense in your API and when your class is "morally const". If you had

void fun(const SceneNode& node); int main() { SceneNode n(...); Bone b; n.setBone(&b); fun(n); assert(*n.bone() == b); // is this a safe assumption? }

If you think that assertion should always hold, then return a const Bone*.

更多推荐

本文发布于:2023-07-15 15:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1115313.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   函数   成员   const   pointer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!