Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a constructor also have an implicit this parameter

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?

like image 735
Anoop Rana Avatar asked Sep 07 '26 14:09

Anoop Rana


2 Answers

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.

like image 165
Ben Voigt Avatar answered Sep 10 '26 03:09

Ben Voigt


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.

like image 45
Caleth Avatar answered Sep 10 '26 04:09

Caleth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!