this 指针不能在构造函数中使用别名:

发布于 2025-01-20 13:06:16 字数 1180 浏览 0 评论 0原文

我正在学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍笑 2025-01-27 13:06:16

的意思是,在构建类对象的过程中,应通过直接或间接从构建器的此间接获得对象的非静态数据成员的访问。否则,未指定通过此类访问读取的值。

因此,this-> a总是很好,就像a一样,它隐含地与this-> a相同。

还可以复制指针this,并通过此访问成员,例如

auto p = this;
b = p->a;

或存储对对象的引用:

auto& r = *this;
b = r.a;

但是在开始时给出的示例中,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 simply a which is implicitly the same as this->a.

It is also ok to copy the pointer this and access members through that, e.g.

auto p = this;
b = p->a;

or to store a reference to the object:

auto& r = *this;
b = r.a;

But in the example given at the beginning, d is the same object as this points to. In other words the name d and *this are aliases for the same object. It is not allowed to use this other alias d to access non-static data members of the class while it is under construction because d was not obtained from this. Or to be more precise, it is unspecified what value such an access will read. So b(d.a) may or may not initialize the b member to the same value as a.

See [class.cdtor]/2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文