const.cpp,RUN
//const用法
#include <iostream>
#include <string>
#include <cmath> //acos需要
const double c_PI = acos(-1.0); //常量,定义好之后不可以修改
//const除了用于上述常量定义,其它主要用与以下函数调用的相关场合
//参数传递
void Show(const std::string &str)
{//函数内部,str是常量,值不可以修改
//str[0]='1';//不可以
std::cout << str << std::endl;
}
void Show(const char* str)
{//str指向的内存不可以修改
//str[0]='1'; //不可以
std::cout << *str << std::endl;
}
class Vector
{
private:
int x, y;
public:
int & X() { return x; }
int X()const { return x; } //const函数,说明该函数不会修改数据成员的值
int & Y() { return y; }
int Y()const { return y; }//const函数,说明该函数不会修改数据成员的值
};
void Test(const Vector & v, Vector & vIn)
{//v是常量,因此函数内部v的值都不能改变,v只能调用Vector类中的const函数
std::cout << v.X() << std::endl;// v.X()是int X()const
vIn.X() = 1; //int & X()返回引用,可以用来表达式的左值
}
int main()
{
return 0;
}