public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Strange way to access member function
       [not found] <1F886735-510D-46DC-9EFE-39B84F4B323D@gmail.com>
@ 2005-05-20  9:26 ` yogesh
  2005-05-20 11:11   ` Lionel B
  0 siblings, 1 reply; 2+ messages in thread
From: yogesh @ 2005-05-20  9:26 UTC (permalink / raw)
  To: gcc-help





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?
What's the point of having static member functions when you can  
access functions like this?

#include <iostream.h>
class Simple {
     public :
              void memfunction()
              {
                 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();

}

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

}


Thanks,
Yogesh


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Strange way to access member function
  2005-05-20  9:26 ` Strange way to access member function yogesh
@ 2005-05-20 11:11   ` Lionel B
  0 siblings, 0 replies; 2+ messages in thread
From: Lionel B @ 2005-05-20 11:11 UTC (permalink / raw)
  To: gcc-help

"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




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-05-20 11:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1F886735-510D-46DC-9EFE-39B84F4B323D@gmail.com>
2005-05-20  9:26 ` Strange way to access member function yogesh
2005-05-20 11:11   ` Lionel B

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).