Tricky Question
-
class c{ public: a(); b(); }; main() { class c *pc = 0; pc->a() //works } class c{ int i; public: c(0):i=0; a(); b(); }; main() { class c *pc = 0; pc->a(); //doesn't work fires exception } why? :(
-
class c{ public: a(); b(); }; main() { class c *pc = 0; pc->a() //works } class c{ int i; public: c(0):i=0; a(); b(); }; main() { class c *pc = 0; pc->a(); //doesn't work fires exception } why? :(
Because you are invoking a function (
a()
) on a pointer that was specifically made invalid (set to 0
).
Who is 'General Failure'? And why is he reading my harddisk?!?
-
class c{ public: a(); b(); }; main() { class c *pc = 0; pc->a() //works } class c{ int i; public: c(0):i=0; a(); b(); }; main() { class c *pc = 0; pc->a(); //doesn't work fires exception } why? :(
What's the definition of function a()? Contrary to popular belief, while you can call a member function through a null pointer, it only works if you don't need to access any member variables or virtual functions (essentially, you don't need to dereference
this
). If a() uses the i integer in the second example, then you are trying to access a variable through a dereferenced null pointer, which obviously will fall over. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky -
What's the definition of function a()? Contrary to popular belief, while you can call a member function through a null pointer, it only works if you don't need to access any member variables or virtual functions (essentially, you don't need to dereference
this
). If a() uses the i integer in the second example, then you are trying to access a variable through a dereferenced null pointer, which obviously will fall over. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky -
class c{ public: a(); b(); }; main() { class c *pc = 0; pc->a() //works } class c{ int i; public: c(0):i=0; a(); b(); }; main() { class c *pc = 0; pc->a(); //doesn't work fires exception } why? :(
Not tricky, it is just basic. Second case will not work... how can you initalize the member variable 'i' while declaring it in the class c,u have to use constructor to initialize it. There is nothing to do with null class pointer. In both cases u have to define the functions a() and b() before calling them. when going gets tough the tough gets going
-
What's the definition of function a()? Contrary to popular belief, while you can call a member function through a null pointer, it only works if you don't need to access any member variables or virtual functions (essentially, you don't need to dereference
this
). If a() uses the i integer in the second example, then you are trying to access a variable through a dereferenced null pointer, which obviously will fall over. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel SpolskyCan U clarify on the matter of this. Please, I know what U told is correct but not able to understand 'you don't need to dereference this'.
-
Not tricky, it is just basic. Second case will not work... how can you initalize the member variable 'i' while declaring it in the class c,u have to use constructor to initialize it. There is nothing to do with null class pointer. In both cases u have to define the functions a() and b() before calling them. when going gets tough the tough gets going
I agree with U. But my intention was about null pointer thing. If U have any please tell me
-
Can U clarify on the matter of this. Please, I know what U told is correct but not able to understand 'you don't need to dereference this'.
SiddharthAtw wrote: Please, I know what U told is correct but not able to understand 'you don't need to dereference this'. When you call a member function, that function has access to an intrinsic pointer called "this", which is a pointer the object you called the method on. So in a hypothetical example:
// we have some class called c int main() { c myObject; myObject.a(); // the this pointer in a() below will be &myObject c *myPointer = new c; myPointer->a(); // the this pointer in a() below will point to the same object as myPointer c *myNullPointer = 0; myNullPointer->a(); // the this pointer in a() below is null, so accessing i through it will result in an access violation. } void c::a() { // these two lines are equivalent. When referring to a member variable in a // member function, there is an implicit "this->" before it. this->i = 1; i = 1; }
-- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky -
class c{ public: a(); b(); }; main() { class c *pc = 0; pc->a() //works } class c{ int i; public: c(0):i=0; a(); b(); }; main() { class c *pc = 0; pc->a(); //doesn't work fires exception } why? :(
You are using undefined behavior, that is why it doesn't work. Calling a member function through a null pointer is invalid, so the fact that two different invalid behaviors are different is a minor matter of compiler specific details. To put it another way, both pieces of code are not valid C++, and consequently the discussion of different behaviors is irrelevant, Visual Studio 8.0 could give you completely different results (in fact a good C++ lint program would detect this error and report it to you.) If it is your goal to call a class defined function without a specific instance of the object you need to use a static member function, viz: class c { public: static void a(); }; main() { c::a(); }
-
I agree with U. But my intention was about null pointer thing. If U have any please tell me
As far as VC++ 6.0 is concerned it is working i.e. null pointer can be used to call the member function but it is not advisable. it may work in few compilers but it is not standard, u shud always initialize the pointer to some valid memory location of this class C type that is what we call initialization of pointer. it could be any pointer for that matter u shud make it point to a valid memory location of its type. when u go for big programs, in the sense, if u use lot of pointers with huge data and dereference it, u may get into Access violation errors and some ugly errors. Please feed the pointers with valid memory location(food) and ask it to work. when going gets tough the tough gets going...
-
SiddharthAtw wrote: Please, I know what U told is correct but not able to understand 'you don't need to dereference this'. When you call a member function, that function has access to an intrinsic pointer called "this", which is a pointer the object you called the method on. So in a hypothetical example:
// we have some class called c int main() { c myObject; myObject.a(); // the this pointer in a() below will be &myObject c *myPointer = new c; myPointer->a(); // the this pointer in a() below will point to the same object as myPointer c *myNullPointer = 0; myNullPointer->a(); // the this pointer in a() below is null, so accessing i through it will result in an access violation. } void c::a() { // these two lines are equivalent. When referring to a member variable in a // member function, there is an implicit "this->" before it. this->i = 1; i = 1; }
-- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel SpolskyThanx for making it Crystal Clear.