2011-07-05 25 views
1

可能重复:
Is there a difference in C++ between copy initialization and direct initialization?两个简单的变量初始化的问题

是否有任何区别,

int x(5); 
int x = 5; 

&什么之间的区别,

int const x(5); 
const int x(5); 
+2

为什么这个标签Ç –

+0

因为我想知道这两种观点 –

+1

如果问题只是第一部分,那么投票可能是正确的,但如果你按照问题的第二部分回答,那么*这个投票是错误的* –

回答

2

一个非常好的规则:

读声明从右到左。

(见Vandevoorde/Josutiss “C++模板:完全指南”)

如:

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. 

自从我遵循这个原则进行的拇指,我从来没有曲解这种声明再次。

(:sisab retcarahc-REP无吨,sisab nekot-REP无薄膜电致发光-OT-thgir NAEM我hguohT:?潮

+2

!! dekicw ylpmis –

+0

@iamcreasy:在评论中有一个错字。 –

+0

@ martinho-fernandes'从右至左阅读.' –

1

除非您使用参数,否则两者都不完全相同。这只是编码风格的问题。

[注:但下面还是不一样的:

int x; // x is variable 
int x(); // x is function declaration 

]

+0

这是一个更好的答案,因为它引发了一个有用的警告。 – Benoit

0

没有区别。替代方法将变量初始化为一个值。

但是你在你的系统上试过这个吗?你得到了什么?

+0

我试过了,他们是一样的。我认为一些技术细节可能会潜伏在后面。 –

-1
int x(5); 
int x = 5; 

没有区别。

int const x(5); 
const int x(5); 

没有区别。

int x = 5; 
const int *p = &x; 
int *const q = &x; 

这里是pq的差异。使用p您无法更改x的值,但可以将新地址分配给p。使用q,您不能指向任何其他内存位置,但可以使用q修改x

+0

你的第三个例子没有什么区别。 –

+0

@martinho fernandes:固定。谢谢。 :) – Donotalo

+0

-1最后一段完全错误。 –

0

然而,有在C++中没有差异,在C, - int x(5);不允许

4

你显示4行中,仅有1有效C.

int x(5);  /* INVALID C */ 
int x = 5; 
int const x(5); /* INVALID C */ 
const int x(5); /* INVALID C */ 

我不知道意思其他语言中的无效行。在C中,有效行的含义是创建名称为x,类型为int,值为5的对象。


要定义一个“常量”(更好:一个“只读”)对象,C语法是

const int x = 5; 
int const x = 5; 

x不能在仅常数允许的地方使用。x不是一个常数:它的类型是const int

#define CONSTANT 42 
enum /*unnamed*/ { First=1, Second, Third }; 
switch (var) { 
    case x: break; /* invalid use of x; only constants allowed in case labels */ 
    case 15: break; /* ok; 15 is a constant */ 
    case CONSTANT: break; /* ok; CONSTANT is a constant with value 42 */ 
    case Second: break; /* ok; Second is a constant with value 2 */ 
} 
+0

是,'const int x = 5'在C中有效吗? –

+0

是的,它会创建一个名为'x'的对象,其值为'5',限定类型为'const int'。注意(非限定或基本)类型仍然是'int';它不会创建一个常量(例如,在案例标签中可用):它会创建一个无法通过该标识符进行更改的对象。 – pmg

+0

使用'case x:break;'我必须使用'const int x = 5;'? –

1

对象关于const关键字:

  • 它使常量什么,立刻将其
  • 如果有没有到左边,那么它适用于右边

所以,int const iconst int i是相同的。

但是,int *const iconst int *i是不一样的:在第一个表达式中,指针是常量,在第二个表达式中,指向的整数是常量。 Als int const* i相当于第二个。

+0

*性格,哪一方首先工作?左还是右? –

+0

'*'或者遵循**类型名称**:在这种情况下,它是一个类型修饰符(将类型修改为指针类型),或者遵循**表达式**:在这种情况下,它是乘法运算符,或者在绑定到指针类型的表达式之前:它是** dereference **运算符。没有这样的规则。 – Benoit

+0

int * const i':我是一个const指针,它指向一个非const int&'int const * i':我是一个指向const int的指针。它是正确的? –

2

首先,内置类型没有区别。对于班级类型,T x = y表示法需要一个拷贝构造函数,T x(y)表示法不需要。 编辑:另外,正如其中一位评论者指出的,如果构造函数T(y)被声明为explicit,则T x = y将不起作用。

对于第二,有没有什么区别,只要编译器而言,但第二(const int)会导致混乱在许多情况下,尤其是当typedef s的参与,如:

typedef int* IntPtr; 
IntPtr const cpi1; // Const pointer to a non-const int. 
const IntPtr cpi2; // Also a const pointer to a non-const int. 

在两个定义,const适用于指针。

由于这种混淆,通常认为更好的做法是将const放在修改后的位置。现在—很长一段时间,把const放在一开始是平常的,结果是这种做法很普遍。关于const拇指

+1

'const'在开始时也是通常用标准写的。 –

+0

“对于类类型,”T x = y“表示法需要一个拷贝构造函数,”T x(y)“表示法不需要。”事实并非如此。两者都需要一个拷贝构造函数。 'T x = y'看起来像是任务,但不是一个。 –

+0

@Fernandes是的,标准是基于ARM的。它似乎没有什么值得麻烦改变(还有一些老的程序员习惯于“const int”,如果我们改变了,他们会觉得不舒服)。 “普遍接受”的最佳实践已经改变为支持'int const',但不会因为使用const int而被批评,或者值得改变现有代码中的做法。 –