动漫设计与制作好学吗,西安seo网站推广优化,jsp电商网站开发流程,无锡建设银行网站上一篇介绍了const修饰的变量或者指针的含义#xff0c;这篇我们介绍const修饰的函数以及函数参数含义。 首先我们看一个例子 class Dog{int age;string name;
public:Dog(){age 3;name dummy;}void setAge(const int a){age a;a;}
};int main(){Dog d;in…上一篇介绍了const修饰的变量或者指针的含义这篇我们介绍const修饰的函数以及函数参数含义。 首先我们看一个例子 class Dog{int age;string name;
public:Dog(){age 3;name dummy;}void setAge(const int a){age a;a;}
};int main(){Dog d;int i 9;d.setAge(i);cout i endl;
} 在上面例子中如果setAge中参数不加上const则可能通过该函数修改i的值如果这不是我们想要的那么就可以通过对参数使用const。编译的时候会报错。 const在C中是非常常用的一个修饰我们在合适的时候就要尽量用上这样的修饰符可以增加代码的健壮性和可读性。 下面我们再看一个例子如果我们将function参数上面的引用符去掉的话又会有什么变化呢 class Dog{int age;string name;
public:Dog(){age 3;name dummy;}void setAge(const int a){age a; // compile error// a;} void setAge(int a){age a; // compile error// a;} };int main(){Dog d;int i 9;d.setAge(i);cout i endl;
} 这样自然编译成功这里需要学习的是如果将引用去掉那么就是值传递i做了一个值拷贝给了这个函数自己不会发生变化这个C初学者也是明白的道理。 如果我们将上面setAge函数的参数的const去掉作为函数重载那么编译也会错误因为C在重载的时候必须要有不同的参数表而const int和int会被编译器认为是同种类型。 返回值为const的函数 class Dog{int age;string name;public:Dog(){age 3; name dummy;}// const parametersvoid setAge(const int a) {age a; coutconstendl;}void setAge(int a) {age a; coutnon-constendl;}// const return value_compconst string getName(){return name;}// const functionavoid printDogName() const {cout const endl;}void printDogName() {cout non-const endl;}
};int main(){Dog d;const string n d.getName();cout n endl; d.printDogName(); Dog d2; d2.printDogName();
} output: dummy const non-const 上面例子非常清楚地展示了const修饰的function的作用效果const如果在function后面表示该function会在该对象为const时调用。需要注意的是如果类的成员函数修饰为const那么该函数中只能调用const函数也就是说上面的const函数不能调用getName因为getName函数不是const函数。 如果把上面printDogName的const去除是不是表示该函数被重载了呢答案是肯定的那么问题来了 什么时候const修饰的函数被调用又什么时候没有const修饰的函数被调用呢大家可以写一写弄清楚这里就直接公布答案了 当Dog为const时const函数会被优先执行当Dog不是const时会优先执行非const函数当然前提是这两个函数都要有如果没有函数重载那么就会调用唯一的那个函数不会报错。 刚才说了const修饰的函数的调用规则那么function参数是否有const的调用规则如何呢规则如下如果传入的参数为const修饰的那么将会调用函数参数中有const修饰的那个。其实这个简单的规则还可以细说将会以Lvalue和Rvalue在后续的博文中继续说明。 转载于:https://www.cnblogs.com/RookieCoder/p/5057174.html