C class functions

Well, that might be a dumb question for many of you. Let me preface with a list in the order of the languages โ€‹โ€‹that I have learned over the past 10 years. [By the way, I understand that some of them are scripting languages]

  • vb 6.0
  • HTML
  • asp
  • Php
  • CSS
  • Javascript
  • managed c ++
  • FROM#
  • C ++
  • FROM
  • ASM

Yes, I know that I started from the opposite end, but I hope this list makes me not criticize me for this, hehe.

QUESTION: Do classes exist in the regular 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 about simple C on the Internet, since most things are for C ++. In addition, I am interested to know any tips, tricks or warnings for working with C. Thank you very much in advance.

BTW: I'm interested in C for mobility and speed.

+4
source share
5 answers

Classes in C are most often modeled by structures combined with function pointers. Non-virtual functions can be passed along with a pointer to a structure, for example:

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

But the real fun begins when you insert pointers into a structure; this means that objects of the same โ€œclassโ€ can have different โ€œmethodsโ€. The strongest syntax flaw is that the specified function does not automatically know for which object it is being called. Thus, the object must also be transferred, which allows you to type a little text than this is usually 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 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 slightly weaker than the inheritance model provided by languages โ€‹โ€‹such as Java. An instance of Animal may have any behavior at all, and not one of a specific set of actions, depending on its subclass. To make things a little more rigorous, methods are usually combined into a structure called vtable (virtual function table). One virtual table is ready for each subtype, and the corresponding one points to an instance.

Note that none of this directly helps you to have different fields for each subtype - it is more complicated (especially syntactically) and can be done either by casting the object to its first member, for example:

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

Or using opaque pointers like

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

If additional fields are hidden behind this void * pointer and are accessible through methods specific to this view.

+5
source

C has no classes. This was one of the reasons for creating C++ , in addition to overloading functions, operator and template overloading.

Of course, code acting as a class was sometimes written long before C++ existed:

 typedef struct class1 class1; struct class1 { int (*constructor) (class1 *this); int (*destructor) (class1 *this); ... }; 
+4
source

C has no classes, and structures in C cannot have functions inside them.

+1
source

There are many differences between the structures of C and C ++, and yes C has no classes.

For example, consider the following code:

 struct demo { int a;//public by default void set()// valid in C++, invalid in C { //do something with a } }; 

Other differences between C and C ++ structures:

-> Constructors and destructors for the purpose of initializing and destroying objects.

-> Support for operator overloading, templates, exception handling, etc. in C ++ structures.

Properties shared between structures in C and POD structures in C ++:

1) Data members are allocated so that later members have higher addresses within the object, unless they are separated by an access specifier.

2) Two types of POD structures are compatible with layouts if they have the same number of non-static data members, and the corresponding non-static data members (in order) have compatible types with layouts. POD-struct may contain an unnamed add-on.

3) A pointer to an object of the POD structure, suitably transformed using reinterpretation, points to its initial element and vice versa, implying that there is no gasket at the beginning of the POD structure.

4) POD-struct can be used with offset macro.

Source -wiki

+1
source

Remember that C was created in 1972 long before the ideas of object-oriented programming were spread. At that time, object-oriented programming was limited to very few languages, such as Simula 67.

So no, C has no classes.

0
source

All Articles