Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: classes in c?

                  
   
  1. #1
    DCEmu Newbie CT_Bolt's Avatar
    Join Date
    Mar 2006
    Location
    Somewhere in Space
    Posts
    39
    Rep Power
    0

    Question classes in c?

    I want to make a class or something like a class in c... know C++ pretty well but, I just started c a little bit ago anyway heres my problem:

    I know this is how to create a class in c++

    class foo
    {
    public:
    foo(int = 0, int = 0);
    int a, b;
    };

    foo::foo(int c, int d)
    {
    a = c;
    b = d;
    }


    could someone please show me how to convert the c++ class into c

  2. #2
    Dream Coder
    Join Date
    Apr 2004
    Location
    Miami, FL
    Age
    37
    Posts
    4,675
    Rep Power
    50

    Default

    You can't make a class in C. What you can do, is something like this:


    struct foo_t{
    int a = 0;
    int b = 0;
    } foo;
    //The above makes an 'object' of type foo_t, called foo. you can then do this:

    void populate_foo(int c, int d){
    foo.a = c;
    foo.b = d;
    }

    Basically if you are taking a whole class structure and converting it to C you can usually create a struct with all the data the class usually held. You can also insert objects of type returntype (* functionname) (type name, type name ...); which would be a pointer to a function. With something like that, what you could do is have something like....

    int add_foo_t(foo_t thefoo){
    return thefoo.a+thefoo.b;
    }
    then make the struct be:

    struct foo_t{
    int a = 0;
    int b = 0;
    int (*add)(foo_t);
    } foo;

    and the init be:

    void init_foo(int c, int d){
    foo.a = c;
    foo.b = d;
    foo.add = add_foo_t;
    }

    now later on you can call (foo.add)(foo); to get a+b.

    Hope these examples weren't too much, I realize I kind of went overboard compared to what you asked for =P.

  3. #3
    DCEmu Newbie CT_Bolt's Avatar
    Join Date
    Mar 2006
    Location
    Somewhere in Space
    Posts
    39
    Rep Power
    0

    Thumbs up Thank You!

    Thank you so much!!! This is exactly what I was looking for... your so kind. I'm glad you went overboard also. It's not too much it's perfect... I may have more trouble with it in the future but, this is just what i need for now... THANK YOU AGIAN! :thumbup:

  4. #4
    Dream Coder
    Join Date
    Apr 2004
    Location
    Miami, FL
    Age
    37
    Posts
    4,675
    Rep Power
    50

    Default

    Another small point. If you typedef a struct, then you can create new ones later.

  5. #5
    DCEmu Newbie CT_Bolt's Avatar
    Join Date
    Mar 2006
    Location
    Somewhere in Space
    Posts
    39
    Rep Power
    0

    Default Ok...

    You mean like this?

    typedef struct foo_t{
    int a = 0;
    int b = 0;
    } foo;

    or no?

    if not could you please show me an example?

  6. #6
    GP2X Coder/Moderator
    Join Date
    Jan 2006
    Posts
    1,678
    Rep Power
    83

    Default

    You can create new instances of the struct (using quzar's example) by doing the following:

    struct foo_t goo;
    struct foo_t dfhdsflhf;
    struct foo_t etc;

    They are all instances of foo_t.

    By using typedeft like so:

    typedef struct foo_t{
    int a = 0;
    int b = 0;
    } foo;

    You create new instances by:
    foo goo;
    foo dfhdsflhf;
    foo etc;

  7. #7
    DCEmu Newbie CT_Bolt's Avatar
    Join Date
    Mar 2006
    Location
    Somewhere in Space
    Posts
    39
    Rep Power
    0

    Question I must have done it wrong...

    Could you please tell me what is wrong with this?

    typedef struct foo_t
    {
    int a = 0;
    int b = 0;
    int (foo_t);
    } foo;

    void init_foo(int c, int d)
    {
    foo.a = c;
    foo.b = d;
    }

    psp-gcc -I. -IC:/devkitpsp/psp/sdk/include -O2 -G0 -Wall -c -o main.o main.c
    main.c:21: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
    main.c: In function 'init_foo':
    main.c:28: error: expected identifier or '(' before '.' token
    main.c:29: error: expected identifier or '(' before '.' token
    make: *** [main.o] Error 1
    I know its probably simple but I just don't get it...

  8. #8
    Dream Coder
    Join Date
    Apr 2004
    Location
    Miami, FL
    Age
    37
    Posts
    4,675
    Rep Power
    50

    Default

    take away the typedef. also the function pointer has to have a name, so int (*add)(foo_t); instead of int (foo_t);

    that should fix it (don't have a dev environment around.

  9. #9
    DCEmu Newbie CT_Bolt's Avatar
    Join Date
    Mar 2006
    Location
    Somewhere in Space
    Posts
    39
    Rep Power
    0

    psp ...

    First of all THANK YOU ALL FOR HELPING ME.

    What I want to do is make my own data type containing other data using '.' tokens. I get these errors when I do this though.

    struct Point_t pn;

    typedef struct Point_t{
    int X, Y;
    } Point;

    void init_Point(int a, int b){
    Point.X = a; // error: expected identifier or '(' before '.' token
    Point.Y = b; // error: expected identifier or '(' before '.' token
    }

    If you know what these errors mean please let me know.

    I need to be able to initialize the Point when I declare it though. How do I do that?

  10. #10
    Dream Coder
    Join Date
    Apr 2004
    Location
    Miami, FL
    Age
    37
    Posts
    4,675
    Rep Power
    50

    Default

    typedef struct Point_t{
    int X, Y;
    } Point;

    struct Point pn;

    void init_Point(int a, int b){
    pn.X = a;
    pn.Y = b;
    }

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •