Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > class and method

Reply
Thread Tools

class and method

 
 
Aniruddha
Guest
Posts: n/a
 
      11-24-2003
How do I simulate(to a minimal extent) the
notion of classes and methods in C ?
 
Reply With Quote
 
 
 
 
Nils Petter Vaskinn
Guest
Posts: n/a
 
      11-24-2003
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

 
Reply With Quote
 
 
 
 
Jirka Klaue
Guest
Posts: n/a
 
      11-24-2003
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
 
Reply With Quote
 
Nils Petter Vaskinn
Guest
Posts: n/a
 
      11-24-2003
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

 
Reply With Quote
 
Jirka Klaue
Guest
Posts: n/a
 
      11-24-2003
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
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      11-24-2003
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
injecting an instance method into another class' class method Chuck Remes Ruby 3 08-12-2010 07:15 PM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
how to call method of the class which contains a pointer to other class method? Pawel_Iks C++ 3 07-31-2007 06:30 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Unit testing - one test class/method, or test class/class Edvard Majakari Python 4 02-25-2005 07:31 PM



Advertisments