Const正确性 - 作为函数参数的Const Pointer [英] Const Correctness - Const Pointer As Function Argument

查看:138
本文介绍了Const正确性 - 作为函数参数的Const Pointer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑像

char* strcpy (char* destination, const char* source);

在(address)source的给定值是const,因为函数的作者想要显示源的值不会被strcpy更改。指针本身不会被strcpy更改为。为什么不写

The given value at (address) source is const because the author of the function wants to show that the value of source will not be changed by strcpy. The pointer itself is not changed by strcpy to. Why not to write

char* strcpy (char* destination, const char* const source);

推荐答案



<解决方案

解决方案

你会看到你是否尝试实现这两个函数,他们实际上是一样的。在这一点上, const 修饰符只对函数体有意义,因为参数是通过值传递的。

You will see if you try to implement both functions, that they are actually the same. A const modifier at that point is only meaningful to the body of the function, since the argument is passed by value anyway.

error: function "strcpy" has already been defined
char* strcpy (char* destination, const char* const source)

这篇关于Const正确性 - 作为函数参数的Const Pointer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆