Thread: psuedo-class

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    psuedo-class

    Is there a way to imitate a class in c, I know something like this will work

    Code:
    #include <stdio.h>
    
    int main(void) {
         struct character {
              int (*print_char)(int num);
         };
     
         struct character my_character;
         my_character.print_char = putchar;
         my_character.print_char('c');
         return 0;
    }
    But how would I get print_char to be assigned automatically, I tried this, but my compiler did not seem to like it
    Code:
    struct character {
         int (*print_char(int num)) = putchar;
    };
    Thanx in advanced.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't. You'll always have to have a "constructor". Such as:
    Code:
    struct character {
              int (*print_char)(int num);
    };
    
    int initcharacter( struct character *c )
    {
        if( c )
        {
            c->print_char = putchar;
            return YAY;
        }
        else
            return BOO;
    }
    
    ...
    
    struct character c;
    initcharacter( &c );
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    On second thought, you may be able to...
    Code:
    struct character
    {
        ...
    }  newcharacter = { putchar };
    And from then on, do something like:
    Code:
    struct character instance = newcharacter;
    But still, you'd have to make an assignment at creation time for it to work. Unless I'm missing something, in which case I'll blame being tired, that could do the trick. Pointers to your structure will still give you a bit of work. But no, there are no C++ style constructors in C.

    And should you forget to make the assignment, and try to use the embedded function pointer, you'll have fun crashes and what not that will be a pain to track down.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    k, thanx.
    I knew you could do this, I think I can work around the constructor though in my project.
    Code:
    #include <stdio.h>
    
    int main(void) {
         struct character {
              int (*print_char)(int num);
         };
     
         struct character my_character = {
              print_char:  putchar, /* gcc extention */
         }
         my_character.print_char('c');
         return 0;
    }
    Or ansi way
    Code:
    #include <stdio.h>
    
    int main(void) {
         struct character {
              int (*print_char)(int num);
         };
     
         struct character my_character = {
              .print_char = putchar, /* ANSI (think) */
         }
         my_character.print_char('c');
         return 0;
    }
    Last edited by chrismiceli; 05-31-2004 at 09:45 PM.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM