JHHK

欢迎来到我的个人网站
行者常至 为者常成

3、内联函数、const

目录

内联函数

使用inline修饰函数的声明或者实现,可以使其变成内联函数
建议声明和实现都增加inline修饰

特点
编译器会将函数调用直接展开为函数体代码
可以减少函数调用的开销
会增大代码体积

注意
尽量不要内联超过10行代码的函数
有些函数即使声明为inline,也不一定会被编译器内联,比如递归函数

内联函数与宏
内联函数和宏,都可以减少函数调用的开销
对比宏,内联函数多了语法检测和函数特性
思考以下代码的区别

#define sum(x) (x + x)    
inline int sum(int x) { return x + x; }  
int a = 10;sum(a++)

//1
inline void inlineFunc(){
    cout<<"inlineFunc"<<endl;
}

//2
#define sum(x) (x + x)
inline int sum1(int x) {
    return x + x;
}


void compare(){
    int a = 10;
    cout<<sum(a++)<<endl;
    cout<<sum1(a++)<<endl;
}

const

一、const是常量的意思,被其修饰的变量不可修改

以下5个指针分别是什么含义?

int age = 10;        
const int * p0 = &age;        
int const * p1 = &age;        
int * const p2 = &age;        
const int * const p3 = &age;        
int const * const p4 = &age;  

上面的指针问题可以用以下结论来解决:
const修饰的是其右边的内容

void constFunc(){
    int age = 10;
    int height = 180;
    
    
    {
        const int * p0 = &age;
        
        // *p0 不可修改 Read-only variable is not assignable
        //*p0 = height;
        
        //p0可修改
        p0 = &height;
    }
    
    
    
    {
        //作用与const int * p0 = &age;相同
        int const * p1 = &age;
    }
    
    
    
    {
        
        int * const p2 = &age;
        
        //*p2可修改
        *p2 = height;
        
        //p2不可修改 Cannot assign to variable 'p2' with const-qualified type 'int *const'
        //p2 = &height;
    }
    
    
    
    {
        const int * const p3 = &age;
        
        //*p3 不可修改 Read-only variable is not assignable
        //*p3 = height;
        
        //p3不可修改 Cannot assign to variable 'p3' with const-qualified type 'const int *const'
        //p3 = &height;
    }
    
    
    {
        //作用 与 const int * const p4 = &age;相同
        int const * const p4 = &age;
    }
}

二、如果修饰的是类、结构体(的指针),其成员也不可以更改

//定义一个结构体
struct Person{
    int age;
    int height;
};


//定义一个结构体类型
typedef struct{
    int age;
    int height;
} Person1;

//通过结构体类型,定义一个结构体指针类型
typedef Person1* Personp;


//直接定义一个结构体指针类型
typedef struct{
    int age;
    int height;
} *PersonPoint;


void constStructFunc(){
    
    {
        const Person person = {10,180};
        //const修饰以下三行代码均不可重新分配
        //person = {11,180};
        //person.age = 11;
        //person.height = 180;
        cout<<"person.age = "<<person.age<<endl<<"person.height = "<<person.height<<endl;
    }
    
    
    {
        Person1 person1 = {18,180};
        Person1 person2 = {19,181};
        const Person1 * p =&person1;
        
        //不可更改*p对应的那块内存中的内容,所以下面3行代码都不可以
        //*p = {19,181};
        //p->age = 19;
        //p->height = 181;
        
        //p可以被重新分配
        p = &person2;
    }
    
    
    {
        //如果是const Person1 * const p = &person1;下面代码会报错
        //p = &person2;
    }
}

总结: const p : p指向的那块内存内数据不能有更改

const p : *p指向的那块内存内数据不能有更改,
比如p->age = 10;p->height = 180;都会导致
p指向的内存内数据发生更改所以不允许

感觉const有点锁定内存数据的意思


行者常至,为者常成!





R
Valine - A simple comment system based on Leancloud.