const函数
来源:互联网 发布:苹果限免软件 编辑:程序博客网 时间:2024/05/19 13:15
const函数
c++的const函数特点:
1. 不能在const函数中修改所在类的对象的数据,因为const函数中的*this是常量,同样只能访问const函数;
2. const函数中只能调用其他的const函数,不能调用非const函数,因为对象调用函数是需要传递对象自己,const函数中的*this是常量,非const函数中的*this是变量,因此不可以调用(除非去除*this的const属性);
Note:使用const_cast后,可以调用在const函数中调用非const函数的
3. const函数与同名的非const函数是重载函数;
4. const对象只能调用const函数 ,但是非const对象可以调用const函数。
cppProj.cpp
- //============================================================================
- // Name : cppProj.cpp
- // Author :
- // Version :
- // Copyright : Your copyright notice
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- #include"people.h"
- using namespace std;
- int main() {
- cout<<"main E."<<endl;
- /*Func f = test();
- int a = f();
- cout<<"a is:"<<a<<endl;*/
- people t;
- t.constTest();
- const people s;
- s.constTest();
- people* w = new people();
- w->constTest();
- const people* x = w;
- x->constTest();
- people* const y = w;
- y->constTest();
- return 0;
- }
people.h
- /*
- * people.h
- *
- * Created on: May 12, 2011
- * Author: root
- */
- #ifndef PEOPLE_H_
- #define PEOPLE_H_
- class people {
- public:
- people();
- void constTest();
- void constTest() const;
- /* void constTest2();
- void const constTest2();*/// method can not start with const
- void test();
- void setAge(int age);
- private:
- int m_iAge;
- };
- #endif /* PEOPLE_H_ */
people.cpp
- /*
- * people.cpp
- *
- * Created on: May 12, 2011
- * Author: root
- */
- #include<iostream>
- #include"people.h"
- using namespace std;
- people::people()
- :m_iAge(-1){
- std::cout<<"people E."<<endl;
- }
- void
- people::constTest() {
- cout<<"constTest E."<<endl;
- setAge(111);
- }
- void
- people::constTest() const{
- cout<<"const method constTest E."<<endl;
- //setAge(111);//error
- //test();//error
- const people * s = this;
- //const people * const s = this;// also ok
- people * t = const_cast<people*>(s);// The operation will remove const property on this.
- //people * t = const_cast<people*>(this);
- t->test();
- t->m_iAge = 3;
- //m_iAge = 4;
- cout<<"const method constTest X."<<endl;
- }
- void
- people::test(){
- cout<<"test E."<<endl;
- }
- void
- people::setAge(int age){
- m_iAge = age;
- }
Output is:
main E.
pe.
constTest E
people E.
const method constTest E.
test E.
const method constTest X.
people E.
constTest E.
const method constTest E.
test E.
const method constTest X.
constTest E.
0 0
- const函数,const变量
- const函数
- const 函数
- const函数
- const函数
- const函数
- const函数
- const 函数
- const函数
- 关键字:Const,Const函数,Const变量,函数后面的Const
- Const,Const函数,Const变量,函数后面的Const
- Const,Const函数,Const变量,函数后面的Const
- Const,Const函数,Const变量,函数后面的Const
- 关键字:Const,Const函数,Const变量,函数后面的Const
- Const,Const函数,Const变量,函数后面的Const(转载)
- Const,Const函数,Const变量,函数后面的Const
- Const,Const函数,Const变量,函数后面的Const
- Const,Const函数,Const变量,函数后面的Const
- RLConnection 和HttpURLConnection
- UITableViewController 编辑功能中的,添加,删除,修改
- HDU_1999 不可摸数
- 遍历给定文件夹中的所有文件以及文件夹 P79
- 天气预报网页版
- const函数
- traceroute命令
- ubuntu 各种窗口操作
- HTTPClient模块的HttpGet和HttpPost
- 使用CAS实现SSO简洁教程
- 并查集 A Bug's Life
- 不可摸数 【杭电-HDOJ-1999】 附题
- 从头开始写项目Makefile(十):make内嵌函数及make命令显示
- BeautifulSoup解析xml文件的使用初步