Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c classes functions

Tags:

c

class

Ok this may be a silly question for many of you. Let me preface this with a list in order of the languages I've learned over the past 10 years. [by the way, I understand that some of these are scripting languages]

  • vb 6.0
  • html
  • asp
  • php
  • css
  • javascript
  • managed c++
  • c#
  • c++
  • C
  • ASM

Yeah I know I started at the complete opposite end, but hopefully the list keeps me from getting criticized to much with this one heh.

QUESTION: Are there classes in plain old C...I know there are structures... Also I would like to know if you can declare functions in C structures/classes(if they exist). I think the answer to both is no, but It's hard to find information on plain C on the internet as most things are for C++. Also, I am interested in knowing any tips, tricks, or warning for working with C. Much thanks in advance.

BTW: I'm interested in C for the purpose of portability and speed.

like image 441
Kelly Elton Avatar asked Dec 12 '09 03:12

Kelly Elton


People also ask

What is class and function in C?

Master C and Embedded C Programming- Learn as you go A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object.

What are functions and classes?

Classes are used to define the operations supported by a particular class of objects (its instances). If your application needs to keep track of people, then Person is probably a class; the instances of this class represent particular people you are tracking. Functions are for calculating things.

Is C first class function?

Functions in C/C++ are not first-class. While (1) and (3) are arguably available through function pointers, (2) is not supported for functions proper.


2 Answers

Classes in C are most often simulated by structs combined with function pointers. Non-virtual functions can be passed alongside a pointer to the struct, like so:

int obj_compare_funct(Obj *a, Obj *b);
int result = compare_two_objects(obj1, obj2, obj_compare_func);

But the real fun starts when you embed the pointers in the struct; this means objects of the same overall "class" can have different "methods". The biggest syntactic downside is that the function pointed to does not automatically know for which object it is being called. So the object needs to be passed as well, which makes for a bit more typing than is normally desirable. For instance:

/***** In the animal.h header file. *****/
typedef struct Animal {
    char *name;
    void (* speak)(Animal *this);   /* The speak "method" */
} Animal;

/* Constructors for various animal types.  Implementation detail: set the animal's speak method to the appropriate one for that animal type. */
extern Animal *make_feline(char *name);
extern Animal *make_rodent(char *name);

/***** Somewhere in zoo.c, which #includes animal.h. *****/
Animal *cat = make_feline("Tom");
Animal *mouse = make_rodent("Jerry");

cat->speak(cat);  /* Print "Tom says meow!" */
mouse->speak(mouse);  /* Print "Jerry says squeak!" */

This example is a bit looser than the inheritance model provided by languages such as Java -- an Animal instance can have any behaviour at all, rather than one of a specific set of behaviours depending on its subclass. To make things a bit stricter, the methods are usually combined into a struct called a vtable (virtual function table). One vtable is pre-made for each subtype, and the appropriate one pointed to from the instance.

Note that none of this directly helps you have different fields for each subtype -- that's trickier (especially syntactically) and can be done either by the trick of casting an object to its first member, e.g.:

/* Can be treated as an Animal if you cast its pointer. */
typedef struct Cat { Animal super; int num_fleas; } Cat;

Or using opaque pointers, e.g.

typedef struct Animal { char *name; void *species_specific_data; } Animal;

Where the extra fields would be hidden away behind that void * pointer, and accessible through the methods particular to that species.

like image 143
Edmund Avatar answered Sep 23 '22 20:09

Edmund


C doesn't have classes. That was one reason for the creation of C++, besides function overloading, operator overloading, and templates.

Of course, code acting class-like was sometimes written long before the existence of C++:

typedef struct class1 class1;

struct class1 {
    int (*constructor) (class1 *this);
    int (*destructor) (class1 *this);
    ...
};
like image 22
wallyk Avatar answered Sep 24 '22 20:09

wallyk