Relationship of the this pointer and (non-)const member function
2011-11-16 16:23
477 查看
Let's see the example first.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202008/31/ae5d3e982bcde021d90d4b7c936d7dae.gif)
Obviously, it is wrong due to the const member function and int& return value. However, how to modify it? The accurate version should be like this:
Through this modification, we can conclude that the type of this pointer is quite different. As we can see, in the const version, the object pointed by this pointer cannot be modified (type: const
A *), while in the common version, it can be modified (type: A *).
class A { public: A() { a = 0; } int& Get() { return a; } void Test() const { this->Get(); } void Test() { this->Get(); } private: int a; }; int main() { A obj; obj.Test(); return 0; }
![](https://oscdn.geek-share.com/Uploads/Images/Content/202008/31/ae5d3e982bcde021d90d4b7c936d7dae.gif)
Obviously, it is wrong due to the const member function and int& return value. However, how to modify it? The accurate version should be like this:
class A { public: A() { a = 0; } int& Get() { return a; } void Test() const { ((A *)this)->Get(); } void Test() { this->Get(); } private: int a; }; int main() { A obj; obj.Test(); return 0; }
Through this modification, we can conclude that the type of this pointer is quite different. As we can see, in the const version, the object pointed by this pointer cannot be modified (type: const
A *), while in the common version, it can be modified (type: A *).
相关文章推荐
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
- what are the semantics of a const member function
- Is the type of “pointer-to-member-function” different from “pointer-to-function”?
- const at the end of function and passing const object in a function call
- Can I get rid of the "This page contains both secure and nonsecure items" warning?
- The trust relationship between this workstation and the primary domain failed
- Item 10.const成员函数 (Meaning of a Const Member Function)
- 错误码: 1418 This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and
- Multiple inheritance and the this pointer
- failed to sync branch You might need to open a shell and debug the state of this repo
- failed to sync branch You might need to open a shell and debug the state of this repo
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary log
- the relationship of view, doc and frame
- The genealogy relationship of the learning and vision guys(机器学习与视觉大牛族谱深度挖掘)
- Question 7: Which of the following correctly identify benefits of the getline() member function for cin over the extraction oper
- [域|Domain] The trust relationship between this workstation and the primary domain failed 此工作站和主域间的信任关系失败
- No orientation specified, and the default is horizontal. This is a common source of bugs when childr
- Cannot find the definition (implementation) of this function
- My priliminary understanding of the relationship between virtual address and physical memory address
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary log