|
Q It is quite possible to invoke member functions through a NULL "this" pointer. As long as the member function is non-virtual, and no member variables are being accessed, the following code works: void Class::Function() {
if (!this) return; // good idea or not?
Even if all the conditions listed above are met, can this code fail? A The code can fail! Consider the following example: class Derived : public Base, public Class {
};
Derived *d=0;
d->Function();
In this particular case, the "this" adjustment doesn't check for NULL, so "this" will be equal to sizeof(Base), which is both non-NULL and invalid. |
| www.self-similar.com |
e-mail: alexei at self-similar.com
|
© 2002 Alexei Lebedev
|