 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alex Guest
|
Posted: Sun Nov 12, 2006 9:06 am Post subject: Can constructor have this pointer? |
|
|
When I saw this code:
class Example {
public:
Example() {
_event = &something;
_event->addClient(*this);
...
}
...
};
I am puzzled? I understand that member non-static functions can have "this"
pointer because they are accessed by the object. When called, the object is
already constructed and "this" pointer points to valid address. However,
can constructor have "this" pointer as above code indicated? I think that
the object has not been constructed so that "this" points to invalid memory
address. But the above code is compiled without problem. What am I
missing? Thanks in advance.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Rahtgaz Guest
|
Posted: Sun Nov 12, 2006 10:10 am Post subject: Re: Can constructor have this pointer? |
|
|
Alex wrote:
| Quote: | When I saw this code:
class Example {
public:
Example() {
_event = &something;
_event->addClient(*this);
...
}
...
};
I am puzzled? I understand that member non-static functions can have "this"
pointer because they are accessed by the object. When called, the object is
already constructed and "this" pointer points to valid address. However,
can constructor have "this" pointer as above code indicated? I think that
the object has not been constructed so that "this" points to invalid memory
address. But the above code is compiled without problem. What am I
missing? Thanks in advance.
|
You are passing a pointer to This. Not This itself.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
flagos Guest
|
Posted: Sun Nov 12, 2006 9:46 pm Post subject: Re: Can constructor have this pointer? |
|
|
When I saw this code:
| Quote: |
class Example {
public:
Example() {
_event = &something;
_event->addClient(*this);
...
}
...
};
|
At constructor time the object instance is already allocated (obvious),
so the this pointer is valid.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Frederick Gotham Guest
|
Posted: Wed Nov 15, 2006 2:14 am Post subject: Re: Can constructor have this pointer? |
|
|
Alex:
| Quote: | When I saw this code:
class Example {
public:
Example() {
_event = &something;
_event->addClient(*this);
...
}
...
};
I am puzzled? I understand that member non-static functions can have
"this" pointer because they are accessed by the object. When called,
the object is already constructed and "this" pointer points to valid
address. However, can constructor have "this" pointer as above code
indicated? I think that the object has not been constructed so that
"this" points to invalid memory address. But the above code is compiled
without problem. What am I missing?
|
What you are missing is that you yourself can write the constructor. You
yourself can decide if the object is in a defined state by the time
"addClient" has been invoked.
--
Frederick Gotham
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Wed Nov 15, 2006 6:02 am Post subject: Re: Can constructor have this pointer? |
|
|
Alex wrote:
| Quote: | When I saw this code:
class Example {
public:
Example() {
_event = &something;
_event->addClient(*this);
...
}
...
};
I am puzzled? I understand that member non-static functions can have "this"
pointer because they are accessed by the object. When called, the object is
already constructed and "this" pointer points to valid address. However,
can constructor have "this" pointer as above code indicated? I think that
the object has not been constructed so that "this" points to invalid memory
address. But the above code is compiled without problem. What am I
missing? Thanks in advance.
|
By the time the body of a constructor executes, storage in memory has
been allocated for the object under construction and all of the
object's members have been completely constructed. So not only does the
"this" pointer reference allocated memory, but it can be used to access
member objects that are already completely constructed. For those
reasons, using of the "this" pointer within a constructor is fine.
Nonetheless, you are correct in thinking that there must be some
difference between code in constructors and code in other member
functions - especially concerning the "this" pointer. And in fact code
in a constructor does have a restriction: while an object is still
under construction, all access to that object must be through the
"this" pointer (either explicitly, or implicitly within a member
function). In other words, were the constructor to call, say, a global
routine that had a pointer or reference to the particular object under
construction and then accessed the object through this external
variable - then the value of any object obtained in this way is
unspecified.[§12.1/16]
So a good rule of thumb to follow is to have the body of a constructor
do as little as possible, that is, to perform only those operations
necessary for the proper initialization of the object; and have the
other member functions implement the rest.
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Earl Purple Guest
|
Posted: Wed Nov 15, 2006 6:33 am Post subject: Re: Can constructor have this pointer? |
|
|
Alex wrote:
| Quote: | When I saw this code:
class Example {
public:
Example() {
_event = &something;
_event->addClient(*this);
...
}
...
};
I am puzzled? I understand that member non-static functions can have "this"
pointer because they are accessed by the object. When called, the object is
already constructed and "this" pointer points to valid address. However,
can constructor have "this" pointer as above code indicated? I think that
the object has not been constructed so that "this" points to invalid memory
address. But the above code is compiled without problem. What am I
missing? Thanks in advance.
|
The code above is valid as it standard, but because Example isn't fully
constructed yet, there are limitations as to what the method addClient
can do with the reference.
In particular, it cannot call a pure virtual function of Example. If it
calls a non-pure virtual function, it will not use the v-table but will
invoke the implementation in the class Example.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Wed Nov 15, 2006 7:42 am Post subject: Re: Can constructor have this pointer? |
|
|
Alex wrote:
| Quote: | class Example {
public:
Example() {
_event = &something;
_event->addClient(*this);
...
}
...
};
I am puzzled? I understand that member non-static functions can
have "this" pointer because they are accessed by the object.
When called, the object is already constructed and "this" pointer
points to valid address. However, can constructor have "this"
pointer as above code indicated?
|
Yes, but it is already completely constructed. In particular all subobjects
(members and baseclasses) have been constructed already. The order of
construction is roughly like this:
1. allocate storage for the object
2. Invoke the ctor of the baseclasses in order of declaration (exception:
virtual baseclasses).
3. Invoke constructor for members in order of declaration.
4. Invoke body of constructor.
5. Invoke derived class' ctor.
Note that how subobjects are initialised can be influenced with the
initialiser list of the ctor but not their order.
So, if you pass 'this' to a baseclass or member in the initializer list you
are passing a pointer to an incompletely initialised object. If these
subobjects do anything more than store that pointer (or a reference) they
will get into trouble. Even the use above might get you into trouble,
because it could be that there are derived classes that are still required
to make the object complete, in particular with respect to pure virtual
functions.
Uli
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Wed Nov 15, 2006 7:49 am Post subject: Re: Can constructor have this pointer? |
|
|
Alex wrote:
| Quote: | When I saw this code:
class Example {
public:
Example() {
_event = &something;
_event->addClient(*this);
...
}
...
};
I am puzzled? I understand that member non-static functions can have "this"
pointer because they are accessed by the object.
|
And the constructor is a non-static member function.
| Quote: | When called, the object is
already constructed and "this" pointer points to valid address. However,
can constructor have "this" pointer as above code indicated? I think that
the object has not been constructed so that "this" points to invalid memory
address.
|
You're partially right. Before the constructor is called,
memory for the object is allocated, so a pointer to that memory
can definitly exist. And the constructor needs that pointer, in
order to initialize the memory. On the other hand, the object
is not yet fully constructed, and some things you do with the
this pointer in a constructor can get you in deep trouble. For
example, if someone derives from Example, and the addClient
function takes its parameter by value, and not by reference
(highly unlikely, IMHO), you're definitely going to get into
trouble. On the other hand, if addClient takes its parameter by
reference, and doesn't do anything that would require a fully
constructed object, there is no problem. Similarly, if there is
no inheritance, and the constructor has done enough before the
call so that the object will behave like a fully constructed
object, there is no problem. (Given the names "event" and
"client" in the above, I'd guess that the first is probably the
case here. And it's a fairly well established idiom.)
In sum, it's legal to use "this" in the constructor, but it is
the programmers responsibility to ensure that the resulting uses
all work.
--
James Kanze (Gabi Software) email: james.kanze (AT) gmail (DOT) com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|