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
Do ctors have an implicit this parameter? This first question can also be phrased as "Do ctors also have an implicit object parameter?".
The standard says that we can use
this
inside a ctor. But where does thatthis
come from. For example, in case of a non-static member function, we know thatthis
comes from the implicit this parameter, but in case of ctors, since ctors don't have an implicitthis
parameter, where does thatthis
that we're allowed to use inside ctor come from.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?
CodePudding user response:
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 implicitthis
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
.
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.
CodePudding user response:
"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.