const char * 作为 C++ 中的函数参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1652126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:46:52  来源:igfitidea点击:

const char * as a function parameter in C++

c++const

提问by Loai Nagati

NOTE: I know there are many questions that talked about that but I'm still a beginner and I couldn't understand the examples.

注意:我知道有很多问题都在讨论这个问题,但我仍然是一个初学者,我无法理解这些例子。

I got a function prototype that goes like this:

我得到了一个像这样的函数原型:

int someFunction(const char * sm);

Here, as you know, const char* means that this function can accept const or non-const pointer-to-char. I tried something like that in the function body:

在这里,如您所知,const char* 意味着该函数可以接受 const 或非常量的指向字符的指针。我在函数体中尝试了类似的东西:

someMemberVar = sm;

someMemberVar is just a pointer-to-char. The compiler gives me an error telling me: cannot convert from const char* to char*.

someMemberVar 只是一个指向字符的指针。编译器给我一个错误,告诉我:无法从 const char* 转换为 char*。

Here, I didn't pass a constant, so either sm or someMemberVar aren't constants. So, what constant the compiler is talking about?

在这里,我没有传递常量,因此 sm 或 someMemberVar 都不是常量。那么,编译器在谈论什么常量?

回答by dirkgently

I'll try to put in simpler terms what others are saying:

我将尝试用更简单的术语来表达其他人的看法:

The function someFunctiontakes a read-only string (for simplicity's sake, though char *could be used in umpteen other cases). Whether you pass in a readonly string to someFunctionor not, the parameter is treated as read-only by the code executing in the context of this function. Within this function therefore, the compiler will try to prevent you from writing to this string as much as possible. A non-const pointer is such an attempt to disregard the read-only tag to the string and the compiler, rightly and loudly informs you of such disregard for its type system ;)

该函数someFunction采用只读字符串(为简单起见,但char *可以在许多其他情况下使用)。无论您是否传入只读字符串someFunction,在此函数的上下文中执行的代码都会将参数视为只读。因此,在此函数中,编译器会尽量阻止您写入此字符串。非常量指针试图忽略字符串和编译器的只读标记,正确而响亮地通知您这种忽略其类型系统的行为;)

What's the difference between: int someFunction(const char * sm) const{...} and this: int someFunction(const char * sm){...}

有什么区别: int someFunction(const char * sm) const{...} 和这个: int someFunction(const char * sm){...}

The first is a function which takes a readonly parameter. The second constwritten after the closing parentheses is valid only for member functions. It not only takes a read-only parameter, but also gurantees to not alter the state of the object. This is typically referred to as design level const.

第一个是采用只读参数的函数。const右括号后的第二个写法仅对成员函数有效。它不仅需要一个只读参数,而且还保证不改变对象的状态。这通常称为设计级别常量。

回答by Pavel Minaev

It is not entirely clear from your question, and I suspect the text of the error that you give is actually wrong, and actually reads:

您的问题并不完全清楚,我怀疑您给出的错误文本实际上是错误的,实际上是:

cannot convert from const char*to char*

不能转换const char*char*

Since you say that

既然你这么说

someMemberVar is just a pointer-to-char.

someMemberVar 只是一个指向字符的指针。

This makes sense. Keep in mind that const char*is actually the same as char const*- that is, it is a pointer to a const char, not a const pointer to char! And you cannot convert a pointer to T constto a pointer to T, because that breaks type safety. Consider:

这是有道理的。请记住,这const char*实际上与char const*- 即,它是指向 const char 的指针,而不是指向 char 的 const 指针!并且您不能将指向的指针转换为指向T const的指针T,因为这会破坏类型安全。考虑:

const char* a = "abc";
char* b = a; // what you're trying to do
b[0] = 'x';  // if you could do it, you could change const data without casts

回答by Nikolai Fetissov

const char*is a pointer to constant char:

const char*是一个指向常量字符的指针:


const char* ptr0; // ptr0 is mutable, *ptr0 is const
char* const ptr1; // ptr1 is const, *ptr1 is mutable

回答by rlbond

If you pass a non-const pointer to someFunction, it is automatically converted to a const pointer. So you can't assign smto someMemberVarbecause that would violate the constness of sm. You could declare someMemberVaras a const char*and your code would work, however, you could not modify what it pointed to.

如果将非常量指针传递给someFunction,它会自动转换为常量指针。所以你不能赋值smsomeMemberVar因为这会违反 sm 的常量性。您可以声明someMemberVar为 aconst char*并且您的代码可以工作,但是,您无法修改它所指向的内容。

回答by Herms

In a comment from one of the other answers you said:

在您说的其他答案之一的评论中:

const char * sm mean that I can pass a const or non-const, so why C++ converts it automatically? That doesn't make sense to me.

const char * sm 意味着我可以传递一个常量或非常量,那么为什么 C++ 会自动转换它呢?这对我来说没有意义。

Between your original question and that comment I think you're misunderstanding how the compiler treats the types.

在您的原始问题和该评论之间,我认为您误解了编译器如何处理类型。

You're correct that a non-const char *can be cast safely to a const char *. However, your method is explicitly taking a const char *. So, while you can pass a char *into the function, the compiler simply automatically casts the argument when making the function call. The actual code in the function doesn't know if the original argument was const or not. The compiler has to go by the actual declaration of the variable you're using, notsome previous variable that had the same value.

您是正确的,非常量char *可以安全地转换为const char *. 但是,您的方法明确采用const char *. 因此,虽然您可以将 a 传递给char *函数,但编译器只是在进行函数调用时自动转换参数。函数中的实际代码不知道原始参数是否为 const。编译器必须通过您正在使用的变量的实际声明,而不是某些具有相同值的先前变量。

If you want to be able to treat non-const char *variables differently (assigning them) then you'll need to define a function that takes non-const char *variables.

如果您希望能够以char *不同的方式处理非常量变量(分配它们),那么您需要定义一个采用非常char *量变量的函数。

回答by Herms

const char * sm is a pointer to a constant character (or array). When you try to assign, someMemberVar, a pointer-to-char, you are trying to point it to a set of constantcharacters. This is the cause for error.

const char * sm 是指向常量字符(或数组)的指针。当您尝试分配someMemberVar,一个指向字符的指针时,您试图将其指向一组常量字符。这就是错误的原因。

回答by Catalin Iacob

In your example sm is const char* so someFunction has a contract with it's caller that it will not modify the memory that sm points to.

在您的示例中 sm 是 const char* 所以 someFunction 与它的调用者有一个合同,它不会修改 sm 指向的内存。

But if you assign sm to someMemberVar and someMemberVar is not const char* you will then be able to modify the memory that sm points to via someMemberVar and the compiler doesn't allow you to do that.

但是,如果您将 sm 分配给 someMemberVar 并且 someMemberVar 不是 const char*,那么您将能够通过 someMemberVar 修改 sm 指向的内存,并且编译器不允许您这样做。