首页 > 其他 > 详细

const函数

时间:2016-06-02 00:43:34      阅读:210      评论:0      收藏:0      [点我收藏+]

const的函数不能对其数据成员进行修改操作。

const的对象,不能引用非const的成员函数。

// test1107.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class aa{
    int num;
public:
    aa(){
        int b =10;
        num = b;
    };
    void out1(){
        cout<<num<<endl;
    }
    void out2() const{
        cout<<num<<endl;
    }
    void out3() const{
        num+=10; //出错,const函数不能修改其数据成员
        cout<<num<<endl;
    }

};
int _tmain(int argc, _TCHAR* argv[])
{
    aa a1;
    a1.out1();
    a1.out2();
    a1.out3();
    const aa a2;
    a2.out1(); // 错误,const的成员 不能访问非const的函数
    a2.out2();
    a2.out3();
    return 0;
}

 

const函数

原文:http://www.cnblogs.com/raichen/p/5551562.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!