class and method

Discussion in 'C Programming' started by Aniruddha, Nov 24, 2003.

  1. Aniruddha

    Aniruddha Guest

    How do I simulate(to a minimal extent) the
    notion of classes and methods in C ?
     
    Aniruddha, Nov 24, 2003
    #1
    1. Advertising

  2. On Sun, 23 Nov 2003 22:30:54 -0800, Aniruddha wrote:

    > How do I simulate(to a minimal extent) the
    > notion of classes and methods in C ?


    struct my_class {
    int blah;
    };


    my_class *new_myclass() {
    struct my_class *this;
    this = malloc(sizeof *this);
    /* error handling here */
    }

    void delete_myclass(struct my_class *this) {
    free(this);
    }

    void my_method(struct my_class *this) {
    do_something_to(this);
    }



    --
    NPV

    "the large print giveth, and the small print taketh away"
    Tom Waits - Step right up
     
    Nils Petter Vaskinn, Nov 24, 2003
    #2
    1. Advertising

  3. Aniruddha

    Jirka Klaue Guest

    Nils Petter Vaskinn wrote:
    > On Sun, 23 Nov 2003 22:30:54 -0800, Aniruddha wrote:
    >
    >>How do I simulate(to a minimal extent) the
    >>notion of classes and methods in C ?

    >
    > struct my_class {
    > int blah;
    > };
    >
    > my_class *new_myclass() {


    struct my_class *new_myclass() {

    > struct my_class *this;
    > this = malloc(sizeof *this);
    > /* error handling here */


    return this;

    > }
    >
    > void delete_myclass(struct my_class *this) {
    > free(this);
    > }
    >
    > void my_method(struct my_class *this) {
    > do_something_to(this);
    > }


    You may want to add function pointers to struct my_class to
    emulate the C++ notion even better.

    Jirka
     
    Jirka Klaue, Nov 24, 2003
    #3
  4. On Mon, 24 Nov 2003 13:56:57 +0100, Jirka Klaue wrote:

    > Nils Petter Vaskinn wrote:
    >> On Sun, 23 Nov 2003 22:30:54 -0800, Aniruddha wrote:
    >>
    >>>How do I simulate(to a minimal extent) the
    >>>notion of classes and methods in C ?


    >> my_class *new_myclass() {

    >
    > struct my_class *new_myclass() {


    doh.

    >
    >> struct my_class *this;
    >> this = malloc(sizeof *this);
    >> /* error handling here */

    >
    > return this;


    doh!

    I need to proofread my posts. But I think I got the general idea across.

    > You may want to add function pointers to struct my_class to
    > emulate the C++ notion even better.


    Depending on the definition of "minimal extent". But for every step
    towards "real" classes it becomes more complex and the OP should consider
    actually using C++ instead.


    --
    NPV

    "the large print giveth, and the small print taketh away"
    Tom Waits - Step right up
     
    Nils Petter Vaskinn, Nov 24, 2003
    #4
  5. Aniruddha

    Jirka Klaue Guest

    Nils Petter Vaskinn wrote:
    > On Mon, 24 Nov 2003 13:56:57 +0100, Jirka Klaue wrote:

    ....
    >>You may want to add function pointers to struct my_class to
    >>emulate the C++ notion even better.

    >
    > Depending on the definition of "minimal extent". But for every step
    > towards "real" classes it becomes more complex and the OP should consider
    > actually using C++ instead.


    :) Very true.

    Jirka
     
    Jirka Klaue, Nov 24, 2003
    #5
  6. Aniruddha wrote:

    > How do I simulate(to a minimal extent) the
    > notion of classes and methods in C ?


    Take a look at
    The ANSI C Numerical Class Library


    http://www.netwood.net/~edwin/svmtl/

    Also, run-time polymorphism has been discussed at length
    in the comp.lang.c newsgroup. See Google Groups

    http://groups.google.com/

    and search for

    Tisdale Shape group:comp.lang.c.*

    Both Re: "class" in C and Re: C version of C++'s virtual functions
    contain an ANSI C implementation of Bjarne Stroustrups Shape class.
     
    E. Robert Tisdale, Nov 24, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,800
    Thomas Weidenfeller
    Oct 12, 2005
  2. Pawel_Iks
    Replies:
    3
    Views:
    537
    terminator
    Jul 31, 2007
  3. Replies:
    3
    Views:
    936
    Roedy Green
    Jan 28, 2008
  4. Chuck Remes
    Replies:
    3
    Views:
    187
    hemant
    Aug 12, 2010
  5. Dan Stromberg
    Replies:
    1
    Views:
    86
    Steven D'Aprano
    Jun 7, 2014
Loading...

Share This Page