Re: Strange way to access member function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"yogesh" <yogesh.kini@xxxxxxxxx> wrote in message news:4FA1BE5B-F622-4485-9BBE-22CE17783117@xxxxxxxxxxxx
> 
> 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





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux