class and method

N

Nils Petter Vaskinn

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);
}
 
J

Jirka Klaue

Nils said:
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
 
N

Nils Petter Vaskinn

struct my_class *new_myclass() {
doh.


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.
 
J

Jirka Klaue

Nils said:
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
 
E

E. Robert Tisdale

Aniruddha said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top