C++中const用法总结

1.const修饰变量

(1)const修饰变量的语义是要求编译器去阻止所有对该变量的赋值行为。因此,必须在const变量初始化时就提供给它初值:

const int i;
i = 5; // !error
const int j = 10; // ok

(2)这个初值可以是编译时即确定的值,也可以是运行期才确定的值。如果给整数类型的const变量一个编译时初值,那么可以用这个变量作为声明数组时的长度:

const int COMPILE_CONST = 10;
const int RunTimeConst = cin.get();
int a1[COMPLIE_CONST]; // ok in C++ and error in C
int a2[RunTimeConst]; // !error in C++

(3)文件域的const变量默认是文件内可见的,如果需要在b.cpp中使用a.cpp中的const变量M,需要在M的初始化处增加extern:
(一般认为将变量的定义放在.h文件中会导致所有include该.h文件的.cpp文件都有此变量的定义,在链接时会造成冲突。但将const变量的定义放在.h文件中是可以的,编译器会将这个变量放入每个.cpp文件的匿名namespace中,因而属于是不同变量,不会造成链接冲突。)

//a.cpp
extern const int M = 20;

//b.cpp
extern const int M;

2.const修饰指针

(1) 指针本身是常量不可变

(char*) const pContent;
const (char*) pContent;

(2) 指针所指向的内容是常量不可变

const (char) *pContent;
(char) const *pContent;

(3) 两者都不可变

const char* const pContent;
const int *p1; // p1 is a non-const pointer and points to a const int
int * const p2; // p2 is a const pointer and points to a non-const int
const int * const p3; // p3 is a const pointer and points to a const it
const int *pa1[10]; // pa1 is an array and contains 10 non-const pointer point to a const int
int * const pa2[10]; // pa2 is an array and contains 10 const pointer point to a non-const int
const int (* p4)[10]; // p4 is a non-const pointer and points to an array contains 10 const int
const int (*pf)(); // pf is a non-const pointer and points to a function which has no arguments and returns a const int

3.const修饰引用

const修饰引用,引用不可被赋值,但真身可以被赋值,赋值后真身和引用都变化。

int a = 1;
int b = 2;
const int& aa = a; //ok,引用不区分类型
aa=b; //error, ri为常量
a=b; //ok,=右边的变量与引用无关

4.const修饰函数参数

(1)void function(const int Var);修饰普通参数,表示形参在函数中不能被修改。

#include<iostream>
using namespace std;

int max(const int a, int b)
{
    a++;//错误,不能修改形参a的值。
    b++;//可以修改b的值
    return (a > b) ? a : b;
}
int main()
{
    int a = 10;
    int b = 20;
    int c = max(a, b);

    system("pause");
    return 0;
}

(2)void function(const int* Var);参数指针所指内容为常量不可变,即在子函数中,不能通过指针改变所指向变量的值,和 const int *p;有些类似。

#include<iostream>
using namespace std;
int max(const int *temp1, int *temp2)
{   
    *temp1 = 30;//错误,不能通过指针修改形参a的值。
    *temp2 = 40;//可以修改b的值
    int c =50;
    temp1=&c;//可以改变指针的指向。
    return (*a > *b) ? (*a) : *b;
}
int main()
{
    int a = 10;
    int b = 20;
    int *p1 = &a, *p2 = &b;
    int c = max(p1, p2);

    system("pause");
    return 0;
}

(3)void function( int* const Var);参数指针本身为常量不可变,不可以改变指针的指向,但可以通过指针改变变量的值。与上面那个相反。

#include<iostream>
using namespace std;

int max( int * const a, int *b)
{


    *a = 30;//可以修改形参a的值。
    *b = 40;
    int c = 50;
    a= &c;  //不可以修改a的指向。
    return (*a > *b) ? (*a) : *b;
}
int main()
{
    int a = 10;
    int b = 20;
    int *p1 = &a, *p2 = &b;
    int c = max(p1, p2);
    system("pause");
    return 0;
}

5.const修饰函数返回值

a.const int fun1() //这个其实无意义,因为参数返回本身就是赋值。
b. const int * fun2() //调用时 const int *pValue = fun2();
//我们可以把fun2()看作成一个变量,即指针内容不可变。
c.int* const fun3() //调用时 int * const pValue = fun2();
//我们可以把fun2()看作成一个变量,即指针本身不可变。

一般情况下,函数的返回值为某个对象时,如果将其声明为const时,多用于操作符的重载。通常,不建议用const修饰函数的返回值类型为某个对象或对某个对象引用的情况。原因如下:如果返回值为某个对象为const(const A test = A 实例)或某个对象的引用为const(const A& test = A实例) ,则返回值具有const属性,则返回实例只能访问类A中的公有(保护)数据成员和const成员函数,并且不允许对其进行赋值操作,这在一般情况下很少用到。

6.const修饰类

(1)const修饰类对象表示该对象为常量对象,其中的任何成员都不能被修改。对于对象指针和对象引用也是一样。
(2)const修饰的对象,该对象的任何非const成员函数都不能被调用,因为任何非const成员函数会有修改成员变量的企图。

#include<iostream>
using namespace std;
class Stack
{
public:
    void Push(int elem);
    int Pop();

    int GetCount() const; // const 成员函数

    Stack(int a) :m_num(a);//只能这样,通过初始化列表幅值。

private:
    const int m_num;//生命成员变量,并且成员变量不能被修改。
    int m_data[100];
};

int main()
{
const Stack test(2);
test.Pop();//错误,不能调用非const函数
test.GetCount();//正确,可以调用const函数。
const Stack *test2 = new Stack(2);
test2->Pop();//错误
test2->GetCount();//正确
}

7.const修饰类成员变量

类中的const成员变量可分为两种:非static常量和static常量。

(1)非static常量:

类中的非static常量必须在构造函数的初始化列表中进行初始化,因为类中的非static成员是在进入构造函数的函数体之前就要构造完成的,而const常量在构造时就必须初始化,构造后的赋值会被编译器阻止。

class B {
public:
    B(): name("aaa") {
        name = "bbb"; // !error
    }
private:
    const std::string name;
};

(2)static常量:

static常量是在类中直接声明的,但要在类外进行唯一的定义和初始值,常用的方法是在对应的.cpp中包含类的static常量的定义:

// a.h
class A {
    ...
    static const std::string name;
};

// a.cpp
const std::string A::name("aaa");

一个特例是,如果static常量的类型是内置的整数类型,如char、int、size_t等,那么可以在类中直接给出初始值,且不需要在类外再进行定义了。编译器会将这种static常量直接替换为相应的初始值,相当于宏替换。但如果在代码中我们像正常变量那样使用这个static常量,如取它的地址,而不是像宏一样只使用它的值,那么我们还是需要在类外给它提供一个定义,但不需要初始值了(因为在声明处已经有了)。

// a.h
class A {
    ...
    static const int SIZE = 50; 
};

// a.cpp
const int A::SIZE = 50; // if use SIZE as a variable, not a macro

8.const修饰类成员函数

一些成员函数改变对象,一些成员函数不改变对象。
例如:

int Point::GetY()
{
 return yVal;
} 

这个函数被调用时,不改变Point对象,而下面的函数改变Point对象:

void Point:: SetPt (int x, int y)
{
 xVal=x;
 yVal=y;
} 

  为了使成员函数的意义更加清楚,我们可在不改变对象的成员函数的函数原型中加上const说明:

  class Point 
{ 
 public:
  int GetX() const;
  int GetY() const;
  void SetPt (int, int);
  void OffsetPt (int, int);
 private:
  int xVal, yVal;
};

  const成员函数应该在函数原型说明和函数定义中都增加const限定:

  int Point::GetY() const
{
 return yVal;
}
class Set {
public:
 Set (void){ card = 0; }
 bool Member(const int) const;
 void AddElem(const int);
 //...
};
bool Set::Member (const int elem) const
{
 //...
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值