-
April 4th, 2010, 12:00 AM
#1
Why does this work?
A dynamic object but we assign it the value NULL...So our object is NULL but we can still call the member function showthis() and it works, but why?
#include <iostream>
class myc
{
public:
myc() {}
~myc() {}
void showthis() const {std::cout<<"Hello, World!"<<"\n";}
private:
};
int main(int argc, char**argv)
{
myc *thec = (myc*)NULL;
std::cout<<"address of thec->"<<(void*)thec<<"\n";
thec->showthis();
return 0;
}
-
April 4th, 2010, 12:13 AM
#2
Re: Why does this work?
"object is NULL" ---- Actually it's a pointer that's NULL. There is no object here.
As to why it works, simple: C++ offers only guarantees about what will work. It promises nothing about what will not work. So just because something appears to be doing what you intend does not mean the program is correct!
In this case, since the method doesn't do anything with the "this" pointer, the fact that that pointer is garbage doesn't matter. You cannot be assured of something like this always working, but that's the most likely explanation for the behavior you're seeing.
-
April 4th, 2010, 12:24 AM
#3
Re: Why does this work?
Heh, thanks. But it was more of a test than a question.
I need you to explain why it's working and doing what it does.
-
April 4th, 2010, 12:28 AM
#4
Re: Why does this work?
Originally Posted by Werpd
But it was more of a test than a question.
A test? Then surely you already know the answer?
Originally Posted by Werpd
I need you to explain why it's working and doing what it does.
It does not work. It appears to work because, as Lindley noted, the member function "doesn't do anything with the "this" pointer".
-
April 4th, 2010, 12:48 AM
#5
Re: Why does this work?
Yes I believe the fact that it doesn't use "this" is correct -- more specifically, because it does not use *any* other class members (which would require using "this"). Basically thec->showthis() is equivalent to saying:
showthis(thec);
...where "thec" is a hidden parameter passed as "this" to all non-static class member functions when invoked....so from there it goes to show that showthis() is just fine until you actually attempt to modify non-static data which would require calling up the specific instance of the class in question (i.e. thec/this)...that's where you get the fun screen.
-
April 4th, 2010, 12:18 PM
#6
Re: Why does this work?
Originally Posted by Werpd
Heh, thanks. But it was more of a test than a question.
I need you to explain why it's working and doing what it does.
And what if I have a compiler where this code doesn't "work"? Do I need to explain that also?
You have constructs in C++ that are considered "undefined behaviour" (UB). The code you have is UB, and there is no need to explain anything. That code could run on that compiler using a certain option, and crap out if another compiler option is used. Someone could develop a compiler that could detect code as you've posted, and just issue a diagnostic that the program cannot continue when you run the code.
C++ isn't like any of the other languages, where bad code automatically produces a diagnostic, i.e. similar to Java or C#. Bad code in C++ (or C) means your program can do anything in particular, including run successfully or crash and burn right away.
Regards,
Paul McKenzie
-
April 4th, 2010, 05:38 PM
#7
Re: Why does this work?
Because it's so easy to accidentally call a method on a null pointer, I have found it useful to do this:
Code:
#ifdef DEBUG
#define NULLSAFE() assert(this)
#else
#define NULLSAFE()
#endif
Since I use GCC almost exclusively, I set pointers to NULL in debug mode to catch those situations.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|