const的用法及其重要性

const的引入其实就是告诉编译器和其它程序员(外部引用者更为合适),该值(变量)应该保持不变。 

1、const对象的定义 ·

 const int sunNumer = 10;//常量定义及初始化 

 const i;//ERROR  

2、const 与指针的结合

 char str[] = "test";   

const char* p = str; //non-const p, const  data

char* const p = str;//const p non-const data

 3、const引用

引用简单说来就是绑定的对象的另一名字,指向同一地址。作用在引用对象上的所有操作同步到绑定的对象上。

int temp1 = 1024;

int& retemp1  = temp1;// retemp1 change   temp1 change

int& retemp2 = 512;//ERROR:引用定义必须绑定对象,即引用初始化

const引用:严格术语--const 引用的意思是指向const 对象的引用,即使是int  double 等内置对象。

const int temp1 = 1024;

const int & contemp1 = temp1;

int &  refvalue = temp1;//ERROR对于const 对象的non-const引用 ,refvalue的值变化会引起temp1的变化,但是temp1是常量

4、const 对象作为函数参数,也是const 最具威力的做法

· 如果程序为尝试改变传入参数的值,尽量将参数定义为const变量。一方面防止他人企图修改传入值,同时提高程序效率。

    ##请记住## pass by reference  to const  好于 pass by value。

同时还可以只利用引用返回函数值(如果函数可能返回多个函数,或者用户希望函数结果列表通过参数列表呈现)

5、const 成员函数

把一个成员函数声明为const可以保证其这个成员函数不修改数据成员,同时明确告诉开发者哪些函数可以对象内容,哪些不可以。

const 对象只能调用const 成员函数,但是const 成员函数可以由非const 对象调用。

要将成员函数声明为const 只用在参数列表后面加入const 关键字。

相关代码:

#pragma once
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
	Student(void);
	~Student(void);
	double addScour(const double& sA,const double& sB);
	void subScour(const double& sA,const double& sB, double& retScour);
	void outInfo() const;//const 成员函数
	void outInfoOne();

private:
	int age;
	string name;
	//const int sumStu =100;//ERROR  只有静态常量整型数据成员才可以在类中初始化
	//const int okNum;//ERROR 必须在构造函数基/成员初始值设定项列表中初始化
	static const int staConstNum;//能通过编译 但是应该在类的外面初始化
	const int constNum;
};


#include "StdAfx.h"
#include "Student.h"

const int Student::staConstNum = 10;
Student::Student(void):constNum(10)
{
	
}
Student::~Student(void)
{
}
double Student::addScour(const double& sA, const double& sB)
{
	//sA += 10;//ERROR
	return sA + sB;
}

void Student::subScour(const double& sA,const double& sB, double& retScour){

	retScour  = sA - sB;
}

void Student::outInfo() const{
	//age =10;//ERROR :由于正在通过常量对象访问“age”,因此无法对其进行修改
	std::cout<<"this is a const fucntion  "<<"Constnum:"<<constNum;
}
void Student::outInfoOne(){
	cout<<"this is a non-const fucntion";
}



main.cpp

// Testone.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include "Student.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	Student newOne;
	double scourA = 90;
	double scourB = 100;
	double sum=0;
	sum  = newOne.addScour(scourA,scourB);
	double sub;
	newOne.subScour(scourA,scourB,sub);
	cout<<"sumScour="<<sum<<"\tsumScour="<<sub<<endl;
	newOne.outInfo();
	cout<<"****************"<<endl;

	const Student conOne;
	conOne.outInfo();//right const对象调用const成员函数
	//conOne.outInfoOne();//ERROR:const 对象不能调用非const 成员函数
	
	int a;
	cin>>a;
	const char* p = "abc";
	return 0;
}





  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值