构造函数是否也有隐式 this 参数

发布于 2025-01-20 20:06:44 字数 2840 浏览 0 评论 0原文

我正在学习 C++ 中的类,并且知道非静态成员函数具有隐式 this 参数。我的第一个问题是,构造函数是否也有一个隐式 this 参数,就像非静态成员函数一样。请注意,我并不是在问我们是否可以在 ctor 内部使用 this,因为我已经知道我们可以在 ctor 内部使用 this


接下来,我知道在类 Xconst 限定非静态成员函数中,this 的类型是 const X *。对于非静态成员函数(不带 const 限定),this 的类型为 X*。同样,在 ctor 内部,this 的类型始终为 X*。更深层次的问题来了。

我们知道,当我们调用一个非静态成员函数(例如obj.func())时,名为obj的对象的地址会隐式传递给隐式成员函数。 func 方法的此参数。因此,这解释了“在非静态成员函数的情况下 this 来自何处”。

现在,让我们将同样的事情应用于构造函数。例如,假设我们使用默认 ctor 创建一个 X 类的对象,如下所示:

X x; //does the default ctor also have an implicit this parameter to which the address of x is passed?

我的第二个问题是:同样的事情也会发生在 ctor 上吗?就像,x 的地址被传递给默认构造函数的隐式参数。我目前的理解是,ctors 没有隐式的 this 参数。因此,当我们编写 X x; 时,x 的地址不会作为参数传递,因为该对象尚未创建,因此传递它的地址是没有意义的。地址。但从标准中我们知道,在 ctor 内部我们可以使用 this 指针。所以我的第二个问题本质上是,如果 ctor 没有隐式 this 参数,那么语句 中的 this 在哪里? this->p = 0;从何而来?我们知道,在 C++ 中使用任何名称(例如变量名称)之前,我们必须对该名称进行声明。那么 this 的声明在哪里?在 ctor 的情况下,编译器是否隐式声明 this ?我的意思是,对于非静态成员函数,我可以理解它们将 this 声明为隐式 this 参数,但是在构造函数中会发生什么? 在 ctors 内部,我们如何能够在没有声明的情况下使用名称 this

struct Name 
{
    private:
         int p = 0;
         int k = 0;
    void func()  //func is a non-static member function and so have an implicit this parameter
    {
        this->k = 0; // the "this" here comes from implicit this parameter
    }
    Name()
    {
      this->p = 0;  //where does the "this" comes from here since ctor don't have implicit this parameter
    }
    
};

我的第三个问题是,这就是隐式的概念this 参数是实现细节,还是标准规定非静态成员函数将具有隐式 this 参数。

摘要

  1. ctor 是否有隐式的 this 参数?第一个问题也可以表述为“医生也有隐式对象参数?”。

  2. 标准规定我们可以在构造函数中使用this。但是这个 this 从哪里来呢?例如,在非静态成员函数的情况下,我们知道 this 来自隐式的 this 参数,但在 ctor 的情况下,因为 ctor 没有隐式的 this< /code> 参数,我们允许在 ctor 内部使用的 this 从哪里来。

  3. 隐式 this 参数的概念是实现细节还是标准规定所有非静态成员函数都有隐式 this 参数,在这种情况下,实现也允许构造函数具有隐式 this 参数。

编辑:

这个问题最重要的部分(IMO)是我们如何在 ctor 中使用名称 this ?例如,当我们编写:

this->p = 0; //here "this" behaves like a name

在上面的语句中 this 的行为就像一个名称。我们知道,在 C++ 中使用任何名称(例如变量名称)之前,我们必须对该名称进行声明。那么 this 的声明在哪里?在 ctor 的情况下,编译器是否隐式声明 this ?我的意思是,对于非静态成员函数,我可以理解它们将 this 声明为隐式 this 参数,但是在构造函数中会发生什么? 在 ctor 内部,我们如何能够使用名称 this 而无需声明?

I am learning about classes in C++ and know that non-static member functions have implicit this parameter. My first question is that does constructor also have an implicit this parameter just like non-static member functions. Note that i am not asking whether we can use this inside a ctor as i already know that we can use this inside a ctor.


Next, I know that inside a const qualified non-static member function for a class X, the type of this is const X*. And for a non-static member function(without const qualified), the type of this is X*. Similarly, inside the ctor the type of this is always X*. Here comes the deeper question.

We know that when we call a non-static member function(say like obj.func()), then the address of the object named obj is implicitly passed to the implicit this parameter of the method func. So this explains "where the this comes from in case of non-static member function".

Now, lets apply the same thing to constructors. For example, say we create an object of class X using default ctor like:

X x; //does the default ctor also have an implicit this parameter to which the address of x is passed?

My second question is: Does the same thing happen to ctors? Like, the address of x is passed to an implicit parameter of the default ctor. My current understanding is that ctors don't have an implicit this parameter. So when we write X x;, the address of x is not passed as an argument because the object is not created yet and so it doesn't make sense to pass its address. But from the standard we know that inside a ctor we can use the this pointer. So my second question essentially is that, if ctors don't have an implicit this parameter then where does the this in the statement this->p = 0; come from? We know that before using any name(like variable name) in C++, we must have a declaration for that name. So where is the declaration for this? Does the compiler implicitly declares the this in case of ctor? I mean, in case of non-static member function, i can understand that they have the declaration for this as the implicit this parameter but what happens in ctors? How inside ctors we're able to use the name this without having a declaration for that?

struct Name 
{
    private:
         int p = 0;
         int k = 0;
    void func()  //func is a non-static member function and so have an implicit this parameter
    {
        this->k = 0; // the "this" here comes from implicit this parameter
    }
    Name()
    {
      this->p = 0;  //where does the "this" comes from here since ctor don't have implicit this parameter
    }
    
};

My third question is that is the concept of implicit this parameter an implementation detail or does the standard says that non-static member function will have an implicit this parameter.

Summary

  1. Do ctors have an implicit this parameter? This first question can also be phrased as "Do ctors also have an implicit object parameter?".

  2. The standard says that we can use this inside a ctor. But where does that this come from. For example, in case of a non-static member function, we know that this comes from the implicit this parameter, but in case of ctors, since ctors don't have an implicit this parameter, where does that this that we're allowed to use inside ctor come from.

  3. Is the concept of implicit this parameter an implementation detail or does the standard say that all non-static member functions have an implicit this parameter, in which case, ctors are also allowed by implementations to have an implicit this parameter.

Edit:

The most important part(IMO) of this question is that how are we able to use the name this inside a ctor? For example when we write:

this->p = 0; //here "this" behaves like a name

In the above statement this behaves like a name. And we know that before using any name(like variable name) in C++, we must have a declaration for that name. So where is the declaration for this? Does the compiler implicitly declares the this in case of ctor? I mean, in case of non-static member function, i can understand that they have the declaration for this as the implicit this parameter but what happens in ctors? How inside ctors we're able to use the name this without having a declaration for that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

汹涌人海 2025-01-27 20:06:44

从 C++ 标准的角度来看

该标准仅描述了 this 关键字的语义,而不是其值如何到达那里。这是完全抽象出来的,并且实现在实现它方面具有很大的灵活性。

从理论计算机科学的角度来看

,正在构造的对象的地址(可通过 this 关键字获得)绝对是该对象的初始化过程(在 C++ 中称为“构造函数”)的输入。虚拟基子对象的地址也是如此,C++ 也可以通过 this 关键字提供这些地址,但无法从主输入中计算出来,因此任何此类都必须是附加输入。

请注意,CS 倾向于在过程中使用“参数”,更多地是在模板参数的意义上,而不是动态变量输入。而CS使用“功能”来表示没有副作用的过程。 C++ 构造函数不是 CS“函数”,虽然模板化构造函数是可能的(参数过程),但 this 的值是普通输入而不是参数化。

C++ 构造函数也不是方法——不存在多态目标类型相关的调度,因此特别是 this 输入不用于调度。

从参数传递的 ABI 规则的角度来看,

C++ 构造函数是一种特殊的成员函数。无法为它构造函数指针或以 C 风格调用它; C 函数的 ABI 要求不适用。

如果平台 ABI 显式描述 C++ 行为,则将存在一个或多个规则用于将 this 的值传递给 C++ 构造函数。这些规则可能会也可能不会指定与其他函数参数等效的机制,但每个针对该平台的符合 ABI 的 C++ 编译器都会根据构造函数的特殊规则的要求传递 this 值。

值得注意的是,将 this 传递给构造函数的 ABI 不需要与将 this 传递给其他(非特殊和非静态)成员函数的方式相同,尽管实际的 ABI 很可能对两者使用相同的方法。

From the perspective of the C++ standard

The standard only describes the semantics of the this keyword and not how its value gets there. That's completely abstracted away and an implementation has great flexibility in making it happen.

From the perspective of theoretical computer science

The address of the object under construction, available via the this keyword, absolutely is an input to the initialization procedure (called "constructor" in C++) for the object. So are addresses of virtual base subobjects, which C++ also makes available via the this keyword but cannot be calculated from the main input, so any such must be additional inputs.

Note that CS tends to use "parameter" with procedures more in the sense of template parameters than dynamic variable inputs. And CS uses "function" to mean a procedure without side effects. A C++ constructor is not a CS "function" and while templated constructors are possible (parametric procedures) the value of this is an ordinary input not a parameterization.

Neither is a C++ constructor a method -- there is no polymorphic target-type-dependent dispatch, and so in particular the this input is not used for dispatch.

From the perspective of ABI rules for parameter-passing

A C++ constructor is a special member function. There's no way to construct a function pointer for it or invoke it C-style; ABI requirements for C functions do not apply.

If the platform ABI explicitly describes C++ behaviors, then there will be one or more rules for passing the value(s) of this to a C++ constructor. Those rules may or may not specify a mechanism equivalent to other function arguments, but every ABI-compliant C++ compiler targeting that platform will pass this values as required by the special rules for constructors.

Notably, the ABI for passing this to a constructor isn't required to be equivalent to how this is passed to other (non-special and non-static) member functions, although practical ABIs may well use the same approach for both.

无语# 2025-01-27 20:06:44

标准中没有“隐式此参数”之类的内容。该标准将其称为“隐式对象参数”。

隐式对象参数仅与重载解析相关,它不会“成为”this。另外,this 被定义为与成员函数具有相同的 cv 限定。

ctor 是否有隐式的 this 参数?第一个问题也可以表述为“医生也有隐式对象参数?”。

不,来自 [over.match.funcs]< /代码>

出于重载决策的目的,静态和非静态成员函数都有隐式对象参数,但构造函数没有。

但是这个this从哪里来。

正在构造的对象。

隐式 this 参数的概念是实现细节还是标准规定所有非静态成员函数都有隐式 this 参数,在这种情况下,ctors实现也允许有一个隐式的 this 参数。

隐式对象参数是重载解析规则的一部分,它不影响 this

在 ctor 内部,我们如何能够在没有声明的情况下使用名称 this ?

this 不是一个名称,它是一个语言关键字。语言将非静态成员函数中的 this 定义为纯右值表达式。与 unqualified-id 不同(命名一个对象)在相同的位置,它们是左值表达式。

这就是语言律师的答案。话虽如此,我确实发现“隐式对象参数变成this”是一个有用的心理模型。

回想一下,构造函数(和析构函数)不能被 cv 限定,因此构造函数中没有任何东西可以区分,因此它是否存在并不重要。

There's no such thing as "implicit this parameter" in the standard. The standard calls it an "implicit object parameter".

An implicit object parameter is only relevant to overload resolution, it doesn't "become" this. Separately, this is defined to have the same cv qualification as the member function.

Do ctors have an implicit this parameter? This first question can also be phrased as "Do ctors also have an implicit object parameter?".

No, from [over.match.funcs]

For the purposes of overload resolution, both static and non-static member functions have an implicit object parameter, but constructors do not.

But where does that this come from.

The object being constructed.

Is the concept of implicit this parameter an implementation detail or does the standard say that all non-static member functions have an implicit this parameter, in which case, ctors are also allowed by implementations to have an implicit this parameter.

The implicit object parameter is part of the rules of overload resolution, it doesn't influence this.

How inside ctors we're able to use the name this without having a declaration for that?

this is not a name, it is a language keyword. The language defines this in a non-static member function to be a prvalue expression. Unlike an unqualified-id (that names an object) in the same position, which are glvalue expressions.

That's the language-lawyer answer. Having said that, I do find that "implicit object parameter becomes this" is a useful mental model.

Recall that constructors (and destructors) can't be cv qualified, so there isn't anything for it to distinguish in a constructor, so it doesn't matter if it exists or not.

云淡风轻 2025-01-27 20:06:44

“this”是一个关键字;它不需要声明,但始终在非静态成员函数中可用。

请参阅此处的标准草案。

关键字 this 命名一个指向对象的指针,为该对象调用隐式对象成员函数或评估非静态数据成员的初始值设定项。

请注意,this 背后的机制尚未指定。

"this" is a keyword; it doesn't need declaring, but is always available in non-static member functions.

See e.g. the draft standard here.

The keyword this names a pointer to the object for which an implicit object member function is invoked or a non-static data member's initializer is evaluated.

Note that the mechanism behind this is unspecified.

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