const成员函数如何更改对象的数据? [英] How const member function can change an object's data?

查看:236
本文介绍了const成员函数如何更改对象的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个问题关于 const 关键字在C + +中使用(我是一个新手在C + +,试图找到一个答案SO - 太多的答案,但没有找到一个)。

问题1:在以下成员函数中

  template< class T& 
bool BinTree< T> :: insert(Node< T>& node,const T& val)const
{
node = new Node&
}

我可以更改作为参考传递的类数据。成员函数 const 会怎么样?这是否是真的 const 阻止数据只有明确更改,但允许通过引用更改它?

2.您能否确认我的理解:

  func(const Node< T> * const& node){...} 

在这个函数签名中第2个 const 表示const指针, st const 表示const节点(因此节点是对const节点的const指针的引用)

解决方法

成员函数const是指对于函数, this 对象( BinTree< T> )是const。 (当然 val 是const因为它被标记为。)(编辑:有点令人惊讶,因为 insert 听起来不像你对一个 const 树做的那种函数,因为你不能更新任何内部数据结构。)



您的函数签名是正确的 - 第一个表示指向的东西是const,第二个表示指针本身是const。



















$ b 节点为非常量。因此,您不能直接更改任何成员变量。 ,如果节点恰好是对通过

调用

  foo.insert(foo.root); 

那么 成员变量可以改变,但只能通过节点。编译器甚至不会注意到两个对象 this node 是相关的,因为在它查看函数 insert 时它们不是(只有当调用实际发生时会发生混叠)。 const 只适用于(文字,如果暗示) this ;任何其他指针或引用挂起可以做他们想要的。 >

Yet another question about const keyword usage in C++ (I am a novice in C++, tried to find an answer on SO - too many answers but failed to find the one).
Question 1: in the following member function

template <class T>  
bool BinTree<T>::insert( Node<T>* & node, const T& val ) const  
{
    node = new Node<T>(val);
}

I am able to change a class data which is passed as a reference. What about a member function being const? Is it true that this const prevents data only from changing explicitly, but allows to change it through a reference?
2. Could you confirm my understanding:

func( const Node<T>* const & node ) {...}

in this function signature the 2-nd const means const pointer and the 1-st const means const Node (so the node is a reference to const pointer to const Node)?

解决方案

The member function being const means that, as far as the function is concerned, the this object (the BinTree<T>) is const. (And of course val is const because it is marked such.) (Edit: It's a bit surprising, as insert doesn't sound like the sort of function you would do to a const tree, as you wouldn't be able to update any of the internal data structures.)

Your function signature is correct -- first one means "thing pointed to" is const, second one means pointer itself is const.


EDIT based on further discussion in comments: The function considers the this object to be const, and the node to be non-const. As such, you can't change any of the member variables directly. However, if node happens to be a reference to a member of this (say it was called via

foo.insert(foo.root);

then that particular member variable can be changed, but only through the alias of node. The compiler is not going to even notice that the two objects this and node are related, because at the time that it's looking at the function insert they're not (only when the call actually happens does the aliasing occur). The const only applies to the (literal, if implied) this; any other pointers or references hanging around can do what they want.

这篇关于const成员函数如何更改对象的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆