typedef和常量指针的容器 [英] typedef and containers of const pointers

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

问题描述

下面的代码行编译正好和行为:

The following line of code compiles just fine and behaves:

list<const int *> int_pointers;  // (1)

以下两行不适用:

typedef int * IntPtr;
list<const IntPtr> int_pointers;  // (2)



我得到与

I get the exact same compile errors for

list<int * const> int_pointers;  // (3)



我很清楚,最后一行不合法,因为元素一个STL容器需要分配。为什么编译器解释(2)与(3)相同?

I'm well aware that the last line is not legal since the elements of an STL container need to be assignable. Why is the compiler interpreting (2) to be the same as (3) ?

推荐答案

b
$ b

Short answer:


  1. 是指向常量int的指针列表。

  2. 是int指向的常量指针列表。
  3. 与2相同。

const(和volatile)
当你写它之前,编译器自动在内部重写它:

const (and volatile) should naturally appear after the type they qualify. When you write it before, the compiler automatically rewrites it internally:

const int *

成为

int const *

这是一个指向常量int的指针。这些列表将编译良好,因为指针本身仍然是可分配的。

which is a pointer to a constant int. Lists of these will compile fine since the pointer itself is still assignable.

这篇关于typedef和常量指针的容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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