const指针赋值给指针 [英] const pointer assign to a pointer

查看:1202
本文介绍了const指针赋值给指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能这样做:

char* p = new char[10];

void SetString(char * const str)
{
    p = str;
}


SetString("Hello");

我有一个指向char的const指针,为什么不能将const指针赋值给另一个指针?

I have a const pointer to a char, why can I not assign the const pointer to another pointer?

这看起来不合逻辑,因为通过将它分配给另一个指针,你基本上不会违反char指针的常量。

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?

编辑:
当我编译时,它说错误C2440:'=':不能从'char * const * __ w64'转换'char *'

When I compile this it says "error C2440: '=' : cannot convert from 'char *const *__w64 ' to '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文件:

#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] = '\0';
    len = n;
}

HEADER FILE:

HEADER FILE:

#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);
};


推荐答案

常量指针和常量指针之间有区别。常量指针是一个不能改变的指针(数字 - 内存地址) - 它总是指向通过初始化给出的同一对象:

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:

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

指向常量的指针是一个指针值不能更改:

Pointer to constant is a pointer whose pointed value cannot be changed:

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

- 指针(a)。你可以将指向非const的指针转换为const(b)的poionter。但是你不能将指向const的指针转换为非常量(c):

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 poionter to const (b). But you cannot cast pointer to const to a poionter to non-const (c):

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*'

下面,这不是标准的c ++。但是,这是很常见的。[/ EDIT]

字符串文字

Below, this is not standard c++. However, this is common.[/EDIT]
String literal

"Hello"

被转换为常量指针const( const char * const ):

is converted to constant pointer to const (const char * const):

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

在上面的例子中,第二个是你的情况。将字符串文字作为函数的参数传递时,您尝试使用指针到const 来初始化指针到非const

In above examples the second is your case. You tried to initialize pointer-to-non-const with a pointer-to-const when 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.

总结:

1)如果你转换一个指针一个类型的指针到另一个类型的指针,你不能将指针到const转换为指针到非const。

2)如果你有常量指针,相同的规则适用于其他常数 - 你可以为一个变量赋一个常量,但不能将一个变量赋值给一个常量(除非初始化它)。

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).

// EDIT

正如GMan指出的,C ++ 98标准(§4.2/ 2)允许将字符串文字(它是常量字符数组)隐式转换为非const char指针。这是因为向后兼容性(在C语言中没有常量)。

// 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).

当然,这样的转换可能导致错误,编译器会违反规则并显示错误。但是,GCC在兼容模式下只显示警告。

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.

这篇关于const指针赋值给指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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