class sample
{
public :
sample()
{
cout<<"\n In C'tor";
}

void fun1()
{
cout<<"\n In fun1()";
}

void fun2()
{
cout<<"\n In fun2()"; 
}

~sample()
{
cout<<"\n In D'tor";
}
};

main()
{
sample *s = new sample;
s->fun1();
delete s;
s = NULL;
s->fun2();
}

Hi all, the given code works successfully.. can any1 tell me ... how above code works..waiting 4 ur quick reply..

sample *s, creates a near pointer to the class sample. "new sample" creates an object, and calls the constructor with 0 argument. s->fun1() ; calls function 1.

s-> fun2() calls fun2 since functions dont depend on this object because there is no variable used,
when objects are created space is allocated only to the variables not to functions ( virtual fns are exceptions)

if fun2 would have had cout << n ;
Then there will be an run time error, cause there is no such n.

Thnx brother...but one thing I don't understand tht after deleting and NULLifying pointer ... how we can access fun2() on tht pointer.

Sorry, I went to lunch, & in a haste, I didnt put it the right way.

Lets say you create a class with some variables and functions. Now you create an object to it. like this

class c
{
public: 
    int n ;
    void getn( int n1 ) { n = n1; }
} ;

c cobj ;
would create a fresh copy of the "int n" only, there is no meaning in creating a new copy of the same function, so fn will take there memory while loading. They will live even if there is no object for the class.

The compiler takes the pointer as cobj pointer and calls the fun2 () stored in memory

the fun2() does not use any variable in the class ( ofcourse, you have no variable in the class ) that is why your code worked.

In your class there is no variable. even if you create an object it is not going to take any space( but sizeof ( cobj) will return 1. It makes sense because, c cobj[10] with all having the same address is non-sense)

I guess this shoul explain

yaa..i understand all this..but i dont understand..after deleteing pointer of the class.. how we can access fun2() using deleted pointer.

> how we can access fun2() using deleted pointer.
You can't. Even if it works once, you can't rely on it to work always. You're seeing what's called "undefined behavior", where your compiler doesn't have to follow any rules about what's supposed to happen. The compiler could do what you expect, what Edward expects, what the guy down the street expects, or what nobody expects...like pop up a message box telling you your snickerdoodle is out of alignment. :D

I guess, the term 'delete s' is misleading.
delete s does not delete the pointer, it deletes the object pointed by it.
It would make sense if delete was named as deletethememorypointedby.
You would be happy to use that big of a keyword, won't you!!!
And in this program, even

((sample*) 12) -> fun2() ; // will work

even
((sample*) main ) -> fun2() ; // will work. dont belive me, try as many times as you like.

but, never refernce any class variable, in that funtion or else the program will crash.

ok..as u said, we delete the memory pointed by pointer using 'delete'. but after tht in my prog.. there is a line ----
delete s;
s = NULL;

after this, the pointer becomes NULL, then how it can points fun2()..

ok..as u said, we delete the memory pointed by pointer using 'delete'. but after tht in my prog.. there is a line ----
delete s;
s = NULL;

after this, the pointer becomes NULL, then how it can points fun2()..

The compiler, binds this code. The compiler, belives that there exist an object in the memory NULL (0) & then calls the fun2(), with the 'this' pointer set to NULL(0).

I wrote the following code,

#include<iostream.h>
class sample
{
public :
    sample(){}
    void fun1(){}
    void fun2(){
        cout << this << endl; 
    }
    ~sample(){}
};

main()
{
    sample *s = new sample;
    s->fun1();
    delete s;
    s = NULL;
    s->fun2();
    ((sample*) 12) -> fun2() ;
    ((sample*) main) -> fun2() ;
}

and the out was,
0
12
4190 // address of main

thnx prabhakar...now i understand..thnx a lot.

C++ is intersting, Isnt it I am learning it too, I still have to learn, templates, error handling, and so on. The book I read, says I still got a lot more to learn. All the best to you too.

same to u bro...thnx once again..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.