简单的规则来读取复杂const声明? [英] Easy rule to read complicated const declarations?
问题描述
有关阅读复杂的指针声明还有就是左右规则。
For reading complex pointer declarations there is the right-left rule.
但是这个规则并没有提及如何读常量
修改器。
But this rule does not mention how to read const
modifiers.
例如在一个简单的指针声明,常量
可以通过多种应用方式:
For example in a simple pointer declaration, const
can be applied in several ways:
char *buffer; // non-const pointer to non-const memory
const char *buffer; // non-const pointer to const memory
char const *buffer; // equivalent to previous declartion
char * const buffer = {0}; // const pointer to non-const memory
char * buffer const = {0}; // error
const char * const buffer = {0}; // const pointer to const memory
现在有什么关于使用常量
与指针声明的指针?
Now what about the use of const
with a pointer of pointer declaration?
char **x; // no const;
const char **x;
char * const *x;
char * * const x;
const char * const * x;
const char * * const x;
const char * const * const x;
什么是一个简单的规则来阅读这些声明?
其中申报有意义吗?
And what is an easy rule to read those declarations? Which declarations make sense?
是顺时针/螺旋规则适用?
该方法<一href=\"http://clang.llvm.org/doxygen/classclang_1_1ASTUnit.html#ab18e63a754071c368a0afccbec0de8a8\"><$c$c>ASTUnit::LoadFromCommandLine$c$c>使用为const char **
提供的命令行参数(在LLVM铛源)。
The method ASTUnit::LoadFromCommandLine
uses const char **
to supply command line arguments (in the llvm clang source).
的参数向量参数的getopt()
声明如下:
The argument vector parameter of getopt()
is declared like this:
int getopt(int argc, char * const argv[], const char *optstring);
其中, char * const的的argv []
等同于 char * const的* argv的
在这种情况下。
由于这两种功能使用相同的概念(指针为字符串矢量提供的参数),并声明有所不同 - 显而易见的问题是:为什么他们有什么不同?使其中一个比另一个更有意义?
Since both functions use the same concept (a vector of pointers to strings to supply the arguments) and the declarations differ - the obvious questions are: Why do they differ? Makes one more sense than the other?
的打算应该是:在常量
修改应指定该函数不处理此向量的字符串,不改变载体的结构
The intend should be: The const
modifier should specify that the function does not manipulate strings of this vector and does not change the structure of the vector.
推荐答案
的常量
修改非常容易:它改变什么precedes,除非
没有precedes它。所以:
The const
modifier is trivial: it modifies what precedes it, unless
nothing precedes it. So:
char const* buffer; // const modifies char
char* const buffer; // const modifies *
等。通常,这是最好避免形式,这里没什么precedes
在常量
,但在实践中,你会看到他们,所以你必须
记得,当没有类型$ P $ pcedes的常量
,你必须在逻辑
移动第一类型的后面。所以:
, etc. Generally, It's best to avoid the forms where nothing precedes
the const
, but in practice, you're going to see them, so you have to
remember that when no type precedes the const
, you have to logically
move it behind the first type. So:
const char** buffer;
其实是:
char const** buffer;
,即指针的指针为const char。
, i.e. pointer to pointer to const char.
最后,在函数声明中,一个 []
之后读作 *
之前。
(再次,它可能会更好,以避免这种误导性的符号,但
你会看到它,所以你必须处理它)所以:
Finally, in a function declaration, a []
after reads as a *
before.
(Again, it's probably better to avoid this misleading notation, but
you're going to see it, so you have to deal with it.) So:
char * const argv[], // As function argument
是:
char *const * argv,
指向一个常量指针为char。
a pointer to a const pointer to a char.
这篇关于简单的规则来读取复杂const声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!