By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

class and method

P: n/a
How do I simulate(to a minimal extent) the
notion of classes and methods in C ?
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Nov 13 '05 #2

P: n/a
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
Nov 13 '05 #3

P: n/a
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

Nov 13 '05 #4

P: n/a
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
Nov 13 '05 #5

P: n/a
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.

Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.