const member functions and const return values
2011-11-03 14:17
253 查看
Why there are two function's versions in one class(const and non-const)? Let's have a test now.
![](http://hi.csdn.net/attachment/201111/3/0_1320301516gGE8.gif)
It is clear to see that the const object will match the const version. However, what happens when we don't have the const member function?
![](http://hi.csdn.net/attachment/201111/3/0_1320301735YW5E.gif)
An error occurs here. How about lacking of the non-const member function version?
![](http://hi.csdn.net/attachment/201111/3/0_1320301889g6q1.gif)
It runs well. However, there is a difference--both const!
The conclusion is that const function is more flexible, while the non-const version is relatively strict.
What about the const return values? Let's see.
![](http://hi.csdn.net/attachment/201111/3/0_13203034669n51.gif)
The value has been changed. Notice that it is a private member value. Now try the const version.
![](http://hi.csdn.net/attachment/201111/3/0_13203034985CZr.gif)
In conclusion, through "&" we can change the private value in class, while it doesn't work provided it is a const return value.
#include <iostream> using namespace std; class A { public: A() {} void fun() { cout<<"I am not const."<<endl; } void fun() const { cout<<"I am const."<<endl; } }; int main() { A test1; test1.fun(); const A test2; test2.fun(); return 0; }
![](http://hi.csdn.net/attachment/201111/3/0_1320301516gGE8.gif)
It is clear to see that the const object will match the const version. However, what happens when we don't have the const member function?
#include <iostream> using namespace std; class A { public: A() {} void fun() { cout<<"I am not const."<<endl; } // void fun() const { cout<<"I am const."<<endl; } }; int main() { A test1; test1.fun(); const A test2; test2.fun(); return 0; }
![](http://hi.csdn.net/attachment/201111/3/0_1320301735YW5E.gif)
An error occurs here. How about lacking of the non-const member function version?
#include <iostream> using namespace std; class A { public: A() {} // void fun() { cout<<"I am not const."<<endl; } void fun() const { cout<<"I am const."<<endl; } }; int main() { A test1; test1.fun(); const A test2; test2.fun(); return 0; }
![](http://hi.csdn.net/attachment/201111/3/0_1320301889g6q1.gif)
It runs well. However, there is a difference--both const!
The conclusion is that const function is more flexible, while the non-const version is relatively strict.
What about the const return values? Let's see.
#include <iostream> using namespace std; class A { public: A() { a=0; } int& fun() { cout<<"I am not const."<<endl; return a; } const int& fun() const { cout<<"I am const."<<endl; return a; } void get() { cout<<"Its value is "<<a<<endl; } private: int a; }; int main() { A test1; test1.get(); test1.fun() = 10; test1.get(); return 0; }
![](http://hi.csdn.net/attachment/201111/3/0_13203034669n51.gif)
The value has been changed. Notice that it is a private member value. Now try the const version.
#include <iostream> using namespace std; class A { public: A() { a=0; } int& fun() { cout<<"I am not const."<<endl; return a; } const int& fun() const { cout<<"I am const."<<endl; return a; } void get() { cout<<"Its value is "<<a<<endl; } private: int a; }; int main() { A test1; test1.get(); test1.fun() = 10; test1.get(); const A test2; test2.fun() = 10; return 0; }
![](http://hi.csdn.net/attachment/201111/3/0_13203034985CZr.gif)
In conclusion, through "&" we can change the private value in class, while it doesn't work provided it is a const return value.
相关文章推荐
- Use of ‘const’ in Functions Return Values
- const (Constant) Objects and const Member Functions
- Chapter 5 Data Hiding and Member Functions
- [转]How to get return values and output values from a stored procedure with EF Core?
- Effective STL 21 Always have comparison functions return false for equal values
- Day2-7.Methods,arguments,and return values
- 6.3Return Types and the return Statement &6.5Overloaded functions
- Tuple Types and Multiple Return Values
- const member function can return non-const type member vars
- c++静态成员函数与线程同步(static member functions and thread-safety)
- call different method with the same name, parameters and return value, select which one is called using const symbol
- Tips: wxString as functions returning and const wxString& as functions arguments
- Relationship of the this pointer and (non-)const member function
- 转载:how to automate Microsoft Excel and return the values from a multi-cell range to an array
- How to declare and initialize a static const array as a class member?
- Csharp: Calling Output Parameters and Return Values in ADO.NET
- Item16 Make const member functions thread safe
- Csharp: Calling Output Parameters and Return Values in ADO.NET
- C++ 之const Member Functions
- C++ primer 5th edition:7.1.2节(关于this指针;关于const member functions)