2.简单变量初始化问题 [英] 2 simple variable initialization question
本文介绍了2.简单变量初始化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
之间有什么区别
int x 5);
int x = 5;
&有什么区别,
int const x(5);
const int x(5);
解决方案
c> const :
从右向左读取声明 p>
(参见Vandevoorde / JosutissC ++模板:完整指南)
例如:
int const x; // x是一个常量int
const int x; // x是一个int,它是const
// easy。该规则在下面变得非常有用:
int const * const p; // p is const-pointer to const-int
int const& p; // p是对const-int的引用
int * const * p; // p是指向int的const指针的指针。
自从我遵循这个经验法则,我从来没有再次错误地解释这样的声明。 >
(:sisab retcarahc-rep a no ton,sisab nekot-rep a no tfel-ot-thgir naem I hguohT:tidE
Possible Duplicate:
Is there a difference in C++ between copy initialization and direct initialization?
Is there any difference between,
int x(5);
int x = 5;
& What's the difference between,
int const x(5);
const int x(5);
解决方案
A really good rule of thumb regarding const
:
Read Declarations Right-to-Left.
(see Vandevoorde/Josutiss "C++ Templates: The Complete Guide")
E.g.:
int const x; // x is a constant int
const int x; // x is an int which is const
// easy. the rule becomes really useful in the following:
int const * const p; // p is const-pointer to const-int
int const &p; // p is a reference to const-int
int * const * p; // p is a pointer to const-pointer to int.
Ever since I follow this rule-of-thumb, I never misinterpreted such declarations again.
(: sisab retcarahc-rep a no ton ,sisab nekot-rep a no tfel-ot-thgir naem I hguohT :tidE
这篇关于2.简单变量初始化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文