469,623 Members | 1,928 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Please explain this to me !!


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
Jul 19 '05 #1
2 1818
Abhish wrote:


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.
Jul 19 '05 #2
On Sat, 01 Nov 2003 04:58:55 -0500, Abhish wrote:
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.
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Yash | last post: by
12 posts views Thread by Sanjeev | last post: by
61 posts views Thread by warint | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.