变量初始化(指针和值)
Foo f1 = Foo(); // (1) Ok
Foo f2 = Foo; // (2) Compiler error
Foo *p1 = new Foo(); // (3) Ok
Foo *p2 = new Foo; // (4) Ok. Why??
我想知道为什么有两种初始化指针的方法。看起来有点不一致。是否存在某种逻辑原因?如果有,是什么?或者,也许这是某种遗产?如果是这样,这种符号的起源是什么?
Foo f1 = Foo(); // (1) Ok
Foo f2 = Foo; // (2) Compiler error
Foo *p1 = new Foo(); // (3) Ok
Foo *p2 = new Foo; // (4) Ok. Why??
I was wondering why there exists two ways to initialize pointers. It looks a little inconsistent. Is there some logical reason, and if so, what? Or, maybe it's some kind of legacy? And if so, What is the origin of such notation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
至少可以说,这有点……复杂。
当处理对象时,这两种表示法是等效的。处理原始类型(例如
int
)时,(3)
会初始化(零填充)值,而(4)
则不会(该值将保持未定义状态)。对于自动分配的对象,此:
使用默认构造函数声明并初始化
Foo
对象。这是:使用复制构造函数声明并初始化
Foo
对象,本质上是复制使用默认构造函数构建的临时对象(Foo()
)的值。It's a bit... complicated, to say the least.
When dealing with objects, both notations are equivalent. When dealing with primitive types (such as
int
),(3)
will initialize (zero fill) the value, while(4)
will not (the value will be left undefined).For automatically allocated objects, this:
Declares and initializes a
Foo
object using the default constructor. This:Declares and initializes a
Foo
object using the copy constructor, essentially copying the value of a temporary object (theFoo()
), which is built with the default constructor.我告诉你,这似乎不合逻辑,但如果你考虑一下可能的含义,它就很有意义。
在不知道
Foo
可能是什么的情况下,人类和编译器(这里,人类的视角更重要)都无法确定Foo f2 = Foo;
是否是一个) 创建一个新的临时Foo
对象,并用它复制构造另一个对象,或者 b) 将变量Foo
的值分配给复制构造的对象 (f2)。这对我们来说似乎是显而易见的,因为我们的约定告诉我们,
Foo
必须是一个类型,因为它是大写的,但总的来说,它并不是那么简单(同样:这主要适用于不知道的人类读者)必须记住整个源代码)。(2)
和(4)
之间的区别在于,对于后者,只有一种解释是可接受的,因为new var
(其中>var
是一个变量)不是合法的表达式。I give you that it seems illogical, but if you think about the possible meanings, it makes a lot of sense.
Without knowing what
Foo
might be, neither a human nor the compiler (here, the human perspective is more important) would be able to determine ifFoo f2 = Foo;
is a) creating a new temporaryFoo
object and copy constructing another one with it or b) assigning the value of a variableFoo
to the copy constructed object (f2
). It may seem obvious for us because our convention tells us thatFoo
must be a type as it is capitalised, but in general it is not that simple (again: this mainly applies to a human reader that does not necessarily have the whole source code memorised).The difference between
(2)
and(4)
is that for the latter, only one interpretation is admissible, becausenew var
(wherevar
is a variable) is not a legal expression.对于基于作用域的资源,4 的等价物实际上只是。
造成差异的遗留原因本质上是在 C 中,原始类型(例如 int)没有默认初始化为有用的东西。当时,C++ 出于性能原因继承了这种行为。当然,现在花费的处理器时间微不足道。在 C++ 中,他们引入了一种新的语法,它总是通过括号来初始化。
i
始终保证为 0。i
的值未定义。这与指针类型完全相同,只是有一些新的内容:
The equivalent of 4 for scope-based resources is actually just
The legacy reason for the differences is essentially that in C, primitive types, such as
int
, were not default-initialized to something useful. C++ inherited this behaviour for performance reasons- at the time. Of course, now, it's a trivial amount of processor time to expend. In C++, they introduced a new syntax by which it would always be initialized- the brackets.i
is always guaranteed to be 0.The value of
i
is undefined.This is the exact same as for the pointer variety, just with some
new
: