C++ const 指针赋值给一个指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3316562/
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
const pointer assign to a pointer
提问by Tony The Lion
Why can I not do this:
为什么我不能这样做:
char* p = new char[10];
void SetString(char * const str)
{
p = str;
}
SetString("Hello");
I have a const pointer to a char, why can I not assign the const pointer to another pointer?
我有一个指向 char 的 const 指针,为什么我不能将 const 指针分配给另一个指针?
It just seems illogical, as by assigning it to another pointer, you are not essentially violating the const-ness of the char pointer. Or are you?
这似乎不合逻辑,因为通过将它分配给另一个指针,您实际上并没有违反 char 指针的常量性。还是你?
EDIT: When I compile this it says "error C2440: '=' : cannot convert from 'char *const *__w64 ' to 'char *'"
编辑:当我编译它时,它说“错误 C2440:'=':无法从'char *const *__w64' 转换为'char *'”
(I'm attempting to understand a concept from a book I'm reading. Just cannot get the code to compile.
(我试图从我正在阅读的书中理解一个概念。只是无法编译代码。
CODE:
代码:
int _tmain(int argc, _TCHAR* argv[])
{
MyString *strg = new MyString(10);
strg->SetString("Hello, ");
MyString *secondstr = new MyString(7);
secondstr->SetString("Tony");
strg->concat(*secondstr, *strg);
}
CPP FILE:
CPP文件:
#include "MyStringClass.h"
#include <string.h>
#include "stdafx.h"
#include "MyStringClass.h"
void MyString::concat(MyString& a, MyString& b)
{
len = a.len + b.len;
s = new char[len + 1];
strcpy(s, a.s);
strcat(s, b.s);
delete [] s;
}
void MyString::SetString(char * const str)
{
s = str;
}
MyString::MyString(int n)
{
s = new char[n+1];
s[n+1] = '#include <string.h>
#include <stdio.h>
class MyString
{
private:
char* s;
int len;
public:
MyString(int n = 80);
void SetString (char * const str);
void concat (MyString& a, MyString& b);
};
';
len = n;
}
HEADER FILE:
头文件:
int * const const_pointer = &some_int_var; // will be always pointing to this var
const_pointer = &some_other_var; // illegal - cannot change the pointer
*const_pointer = 2; // legal, the pointer is a pointer to non-const
回答by adf88
There is difference between constant pointer and pointer to constant. Constant pointer is a pointer (a number - memory address) that cannot be changed - it always point to the same object given via initialization:
常量指针和指向常量的指针是有区别的。常量指针是一个不能改变的指针(一个数字 - 内存地址) - 它总是指向通过初始化给出的同一个对象:
const int * pointer_to_const = &some_int_var; // doesn't have to be always pointing to this var
pointer = &some_other_var; // legal, it's not a constant pointer and we can change it
*pointer = 2; // illegal, pointed value cannot be changed
Pointer to constant is a pointer whose pointed value cannot be changed:
指向常量的指针是一个指针,其指向的值不能改变:
int * pointer;
int * const const_pointer = &var;
const int * pointer_to_const;
/* a */
pointer = const_pointer; // OK, no cast (same type)
/* b */
pointer_to_const = pointer; // OK, casting 'int*' to 'const int*'
/* c */
pointer = pointer_to_const; // Illegal, casting 'const int*' to 'int*'
You can always assign constant to variable i.e. const pointer to non-const pointer (a). You can cast pointer to non-const to a pointer to const (b). But you cannot cast pointer to const to a pointer to non-const (c):
您始终可以将常量分配给变量,即指向非常量指针 (a) 的常量指针。您可以将指向非常量的指针转换为指向 const (b) 的指针。但是您不能将指向 const 的指针转换为指向非常量 (c) 的指针:
"Hello"
[EDIT] Below, this is not standard c++. However, this is common.[/EDIT]
String literal
[编辑] 下面,这不是标准的 C++。但是,这是常见的。[/EDIT]
字符串文字
char *pointer = "Hello"; // Illegal, cannot cast 'const char*' to 'char*'
char * const const_pointer = "Hello"; // Illegal, cannot cast 'const char*' to 'char*'
const char * pointer_to_const = "Hello"; // OK, we can assign a constant to a variable of the same type (and the type is 'const char*')
"Hello" = pointer_to_const; // Illegal cannot re-assign a constant
is converted to constant pointer to const (const char * const
):
转换为指向 const ( const char * const
) 的常量指针:
In above examples the second is your case. You tried to initialize pointer-to-non-constwith a pointer-to-constwhen passing string literal as argument of your function. No matter if these pointers are constants or not, it's matter what do they point to.
在上面的例子中,第二个是你的情况。在将字符串文字作为函数的参数传递时,您尝试使用指向常量的指针初始化指向非常量的指针。不管这些指针是否是常量,它们指向什么很重要。
Summary:
1) If you cast a pointer of some type to a pointer of another type, you cannot cast pointer-to-const to pointer-to-non-const.
2) If you have constant pointer, the same rules applies as to other constants - you can assign a constant to a variable but you cannot assign a variable to a constant (except initializing it).
总结:
1)如果将某种类型的指针转换为另一种类型的指针,则不能将指向常量的指针转换为指向非常量的指针。
2) 如果你有常量指针,同样的规则适用于其他常量——你可以将常量分配给变量,但不能将变量分配给常量(初始化除外)。
// EDIT
As GMan pointed out, the C++98 standard (§4.2/2) allows to implicitly cast string literals (which are constant char arrays) to a non-const char pointer. This is because of backward compatibility (in C language there are no constants).
// 编辑
正如 GMan 所指出的,C++98 标准(第 4.2/2 节)允许将字符串文字(它们是常量字符数组)隐式转换为非常量字符指针。这是因为向后兼容(在 C 语言中没有常量)。
Of course such a conversion can lead to mistakes and compilers will violate the rule and show an error. However, GCC in compatibility mode shows only a warning.
当然,这样的转换会导致错误,编译器会违反规则并显示错误。但是,兼容模式下的 GCC 仅显示警告。