this 指针不能在构造函数中使用别名:
我正在学习C ++的继承。然后我遇到以下语句:
换句话说,该指针在构造函数中不能被别名:
extern struct D d;
struct D
{
D(int a) : a(a), b(d.a) {} // b(a) or b(this->a) would be correct
int a, b;
};
D d = D(1); // because b(d.a) did not obtain a through this, d.b is now unspecified
以上示例来自 cppreference 。
我的第一个问题是写道“ 此
不能在ctor中被别名”,但是在上面的示例中,他们已经在评论中写了> b(> a)
是正确的。这似乎对我来说是矛盾的,因为当他们说无法在ctor中被别名时,我知道“
此
不能在ctor”。但是,为什么他们说编写b(this-> a)
如果无法在ctor中使用/易被辩护,则是正确的。初始化器列表不是“在CTOR中”考虑的?
现在让我们查看一个自定义示例:
struct Name
{
private:
int x = 0;
Name(int n )
{
this->x = 4; //is the use of "this" well-defined here?
}
};
我的第二个问题是在上面显示的转换构造函数内的表达式this-> x
的使用吗?我的意思是,由于在我的问题开头的报价的开头,此在ctor中不能被别名,因此这不应有效。
I am learning about inheritance in C++. And i came across the following statement:
In other words, the this pointer cannot be aliased in a constructor:
extern struct D d;
struct D
{
D(int a) : a(a), b(d.a) {} // b(a) or b(this->a) would be correct
int a, b;
};
D d = D(1); // because b(d.a) did not obtain a through this, d.b is now unspecified
The above example is from cppreference.
My first question is that it is written that "this
cannot be aliased in a ctor" but then in the example above, they've written in the comment "b(this->a)
would be correct". This seems to be a contradiction to me because when they said that this
cannot be aliased in a ctor i understood that "this
cannot be used in a ctor". But then why are they saying that writing b(this->a)
would be correct if this
cannot be used/aliased in a ctor. Isn't the initializer list considered "in a ctor"?
Now lets look at a custom example:
struct Name
{
private:
int x = 0;
Name(int n )
{
this->x = 4; //is the use of "this" well-defined here?
}
};
My second question is that is the use of the expression this->x
inside the converting constructor shown above well-defined? I mean since according the quote at the beginning of my question this
can't be aliased in a ctor, so this shouldn't be valid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
的意思是,在构建类对象的过程中,应通过直接或间接从构建器的此间接获得对象的非静态数据成员的访问。否则,未指定通过此类访问读取的值。
因此,
this-> a
总是很好,就像a
一样,它隐含地与this-> a
相同。还可以复制指针
this
,并通过此访问成员,例如或存储对对象的引用:
但是在开始时给出的示例中,
d
是与的对象相同,
指向。换句话说,名称d
和*此
是同一对象的别名。不允许使用此其他别名d
在构建过程中访问该类的非静态数据成员,因为d
未从获得此
/代码>。或更确切地说,未指定这样的访问将读取什么价值。因此,
b(da)
可能会或可能不会将b
成员初始化为与a
相同的值。参见 [class.cdtor]/2 。
What is meant is that during the construction of a class object any access to the object's non-static data members should happen through a pointer/glvalue obtained directly or indirectly from
this
of the constructor. Otherwise the value read by such an access is unspecified.So
this->a
is always fine, as is simplya
which is implicitly the same asthis->a
.It is also ok to copy the pointer
this
and access members through that, e.g.or to store a reference to the object:
But in the example given at the beginning,
d
is the same object asthis
points to. In other words the named
and*this
are aliases for the same object. It is not allowed to use this other aliasd
to access non-static data members of the class while it is under construction becaused
was not obtained fromthis
. Or to be more precise, it is unspecified what value such an access will read. Sob(d.a)
may or may not initialize theb
member to the same value asa
.See [class.cdtor]/2.