Strange way to access member function
Lionel B
lionelbuk@yahoo.co.uk
Fri May 20 11:11:00 GMT 2005
"yogesh" <yogesh.kini@gmail.com> wrote in message news:4FA1BE5B-F622-4485-9BBE-22CE17783117@gmail.com...
>
> Hi,
> This is a piece of code I found while going through a web site..
> Surprisingly it compiles and gives the expected output(prints the
> line), even though the pointer is initialized to NULL.
>
> I am using gcc 3.3 on Mac OS X.
>
> Can any one tell me what's happening?
Undefined behaviour. It works for you. On another system it might simply defrost the fridge instead ;-)
("weirdly", it works for me too)
> What's the point of having static member functions when you can
> access functions like this?
>
> #include <iostream.h>
#include <iostream>
> class Simple {
> public :
> void memfunction()
> {
> cout << "Hello I am in memFunction" ;
std::cout << "Hello I am in memFunction" ;
> }
>
> };
>
> Simple *objSimple = NULL;
>
> 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();
>
> }
You don't need wrapper.
> int main()
> {
> wrapper();
> exit(0);
>
> }
int main()
{
Simple *objSimple = 0;
objSimple->memfunction();
return 0;
}
--
Lionel B
More information about the Gcc-help
mailing list