Accessing a Meber function through a pointer to a class

P

pramod8378

#include <iostream.h>
class Simple {
public :
void memfunction()
{
cout << "Hello I am in memFunction" ;
}
};

Simple *objSimple ;

void wrapper()
{

/* how we are able to access the member function , when we had not
created an object ? then wht's the use of Static meber functions ?
*/
objSimple->memfunction();
}



int main()
{
wrapper();
exit(0);
}

how wrapper function is calling the member function , without creating
the object ?


Thanks in advance .
 
T

Thomas Maier-Komor

#include <iostream.h>
class Simple {
public :
void memfunction()
{
cout << "Hello I am in memFunction" ;
}
};

Simple *objSimple ;

void wrapper()
{

/* how we are able to access the member function , when we had not
created an object ? then wht's the use of Static meber functions ?
*/
objSimple->memfunction();
}



int main()
{
wrapper();
exit(0);
}

how wrapper function is calling the member function , without creating
the object ?


Thanks in advance .

objSimple has not been initialized explicitly. So it is initialized to
zero as it has static storage. Dereferencing a null pointer is undefined
behaviour. So after executing objSimple->memfunction your program is
in error and anything can happen...

Tom
 
K

Karl Heinz Buchegger

[snip]
void wrapper()
{

/* how we are able to access the member function , when we had not
created an object ? then wht's the use of Static meber functions ?
*/
objSimple->memfunction();
}

int main()
{
wrapper();
exit(0);
}

how wrapper function is calling the member function , without creating
the object ?

The answer is:
It seems to work in that specific case but is nevertheless a faulty program.
Anything could have happend and that includes: seems to work.
 
S

Shivanand

A friend of mine had following explaination..

/**
When ever we call an object's function, the compiler converts the call
to something like this...

Check the example..

You have a class base
{

Public:
hello(int x);
};

Object obj is the object of type Base.
Now if you call obj.hello (4) ;
The compiler converts this call to
F1_hello(4, obj).. Check the second argument.. That is object which is
calling the function.
Now if obj was an un initialized pointer, it can be null or having
garbage values.

In your example, they are not altering any data members in the function
"MemFunctuion() "

So the call now becomes
F1_memFunction(objSimple);

Since we don't refer any data members of the class Simple, it works..
If you refer some data members.. It will crash..


This also shows that objects of a class doesn't have individual copies
of member functions.. They share a common set of member functions.
**/
 
J

Jaspreet

how wrapper function is calling the member function , without creating
the object ?


Thanks in advance .

This is undefined behaviour and may work in some cases and maynot in
other. However, I am pretty sure if you try to access a data memebr
instead of the member function in wrapper, you should get a
segmentation fault.
 

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
474,203
Messages
2,571,059
Members
47,668
Latest member
SamiraShac

Latest Threads

Top