Please explain this to me !!

A

Abhish

HI All ,:)

I am a VC++ programmer,and some time My <b>Acumen</b> ask Microsofts
VC++ (Visual Studio VC++ 6.0 )complier to complile my <b>senseless
programs </b> ! :)

<i><b>

See what I have asked this time to compile !!..</b></i>

This is console based program, in which i am trying to call Member
function of My class With a NULL pointer .

And it is working fine....:cool:

Please some one explain this to me ....is this happening of coz Compiler
is fed up with my programs !! :-D



#include "stdafx.h"

#include <iostream.h>



class A

{

public :

int var;

A()

{

cout<<"In Consrtuctor !!";

}



~A()

{

cout<<"\nClass destroyed !!\n";

}



void EvenNullPointerCanCallme(int a);

};





void A::EvenNullPointerCanCallme(int a)

{

int *aa=new int[100];

cout<<"\nHow is that possible !!\n";

delete []aa;

}



int main(int argc, char* argv[])

{

A *nullPtr=NULL;//see i made it NULL



nullPtr->EvenNullPointerCanCallme(2);//How daring i am :)



delete nullPtr;//I cannot call destructor !



return 0;

}





Are member functions statically linked with the Object name !!



Actually i never found any concept of accessing Class member function
with NULL pointers in Any of C++ books Which I have read (Plz let me
know if u people have found it in any book !).



I am grateful to all u peoples who had a look at my program !!

Now see i am getting famous or what :)


--
Abhishek Srivastava
Software engg
India , Noida
:)


Posted via http://dbforums.com
 
L

lilburne

Abhish said:
Actually i never found any concept of accessing Class member function
with NULL pointers in Any of C++ books Which I have read (Plz let me
know if u people have found it in any book !).

The only reason this works is because you aren't accessing
any data in the methods you call. Access the vaiable 'var'
and the program will crash.
 
S

Simon Saunders

This is console based program, in which i am trying to call Member
function of My class With a NULL pointer .

And it is working fine....:cool:

Please some one explain this to me ....is this happening of coz Compiler
is fed up with my programs !! :-D

Technically, calling a member function on a null pointer is undefined
behaviour, so anything might happen. That it works in this case is down to
the way your compiler implements non-virtual member functions: probably
just like a free function with an extra parameter for "this". Your member
function doesn't use any members of the class, hence the "this" pointer
isn't dereferenced and nothing bad happens. Try modifying the function so
that it uses a member variable and see what happens then.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top