I just saw this beauty today...
-
In C++ that makes sense as
this
can beNULL
in some situations. :)Navaneeth How to use google | Ask smart questions
-
In C++ that makes sense as
this
can beNULL
in some situations. :)Navaneeth How to use google | Ask smart questions
-
My c++ is obviously rusty, what situations are those?
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
Call a method passing a null object... In Delphi, the Free method checks that it was called for an object that was really created. It's quite useful, since you can write "MyObject.Free" instead of "if Assigned(MyObject) then MyObject.Free;". A real time saver, and code is so much more readable.
-- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.
-
Call a method passing a null object... In Delphi, the Free method checks that it was called for an object that was really created. It's quite useful, since you can write "MyObject.Free" instead of "if Assigned(MyObject) then MyObject.Free;". A real time saver, and code is so much more readable.
-- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.
C++ lets you set the this object in the parameters instead of always using the object the method is called on?
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
-
C++ lets you set the this object in the parameters instead of always using the object the method is called on?
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
No, but if you write:
int foo ()
{
string *s = null;
return s->length();
}the string::length function will be called with this==null. This could be used so that the null string would behave as the empty string.
-- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.
-
No, but if you write:
int foo ()
{
string *s = null;
return s->length();
}the string::length function will be called with this==null. This could be used so that the null string would behave as the empty string.
-- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.
-
C++ lets you set the this object in the parameters instead of always using the object the method is called on?
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
William has already explained it neatly. Here is another example if you are interested.
class Dummy{
public:
void CheckNull(){
if(!this)
std::cout << "This is NULL!";
}
};Dummy* dummy = 0;
dummy->CheckNull();You can see This is NULL message appearing in the above example. Above code is problematic and as per C++ standard, calls on an uninitialized pointer will lead into unexpected results.
dan neely wrote:
C++ lets you set the this object in the parameters instead of always using the object the method is called on?
No. AFAIK, compiler compiles the
CheckNull()
method likevoid Dummy_CheckNull(Dummy* this);
and the
Dummy
instance we created will be passed to this method. Your code will work fine if you are not using thethis
. Any operation onthis
will fail as it is not initialized. Hope it is clear now! :)Navaneeth How to use google | Ask smart questions
-
No, but if you write:
int foo ()
{
string *s = null;
return s->length();
}the string::length function will be called with this==null. This could be used so that the null string would behave as the empty string.
-- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.
Thereby obscuring the real problem, which is that you have a null object instance you're trying to use. Loads of fun tracking down something like that.
¡El diablo está en mis pantalones! ¡Mire, mire! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog Just Say No to Web 2 Point Oh
-
William has already explained it neatly. Here is another example if you are interested.
class Dummy{
public:
void CheckNull(){
if(!this)
std::cout << "This is NULL!";
}
};Dummy* dummy = 0;
dummy->CheckNull();You can see This is NULL message appearing in the above example. Above code is problematic and as per C++ standard, calls on an uninitialized pointer will lead into unexpected results.
dan neely wrote:
C++ lets you set the this object in the parameters instead of always using the object the method is called on?
No. AFAIK, compiler compiles the
CheckNull()
method likevoid Dummy_CheckNull(Dummy* this);
and the
Dummy
instance we created will be passed to this method. Your code will work fine if you are not using thethis
. Any operation onthis
will fail as it is not initialized. Hope it is clear now! :)Navaneeth How to use google | Ask smart questions
would this be another example?
class NullCall {
public:
char PublicMethod(int x) {
return (char)x; }
};NullCall::PublicMethod(12);
I.E. using the namespace/scope operator '::' ? -Adam
-
would this be another example?
class NullCall {
public:
char PublicMethod(int x) {
return (char)x; }
};NullCall::PublicMethod(12);
I.E. using the namespace/scope operator '::' ? -Adam
TheScientistIsDead wrote:
would this be another example?
I don't think so. Your code produces error "
error: cannot call member function ‘char NullCall::PublicMethod(int)’ without object
". You can compile this by making the method asstatic
but you can't usethis
inside a static method. :)Navaneeth How to use google | Ask smart questions