Thread: Member Functions

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    Question Member Functions

    My teacher has introduced me to Member functions. Unfortunately the example he showed me was for C++. I was wondering if there was a similiar funcionatility in C.

    If so, can someone show me an example?

    example C++
    Code:
    struct test {
        int smile;
        void get_info();
    };
    
    
    int main()
    {
       struct test test;
    
       test.smile = 3;
    
       smile.get_inf();
       return 0;
    }
    
    
    void smile::smile()
    {
      cout<<smile;
    }
    Last edited by Raven; 03-12-2002 at 07:30 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You can get the same functionality as member functions with function pointers in C. The function pointer acts exactly like a pointer to a simple variable and all normal rules apply. C++ member functions have a much simpler syntax though
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Fooby 
    {  
      void (*getInfo) ( const int input ); 
    } static foo;
    
    static void getInfo ( const int input ) 
    { 
      printf ( "The number is %d\n", input ); 
    }
    
    int main ( void )
    {
      foo.getInfo = &getInfo;
      foo.getInfo ( 10 );
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. member functions can't see private data members
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2006, 02:36 PM
  2. issue with member functions
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 09-27-2006, 09:18 PM
  3. Classes and member functions
    By uraliss in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2006, 07:38 AM
  4. Class member variables only readable by member functions?
    By _Elixia_ in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2003, 03:52 PM
  5. trouble defining member functions
    By dP munky in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2003, 08:52 PM