Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Using structs as classes in C without contortions.

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
edbarx
Posts: 5401
Joined: 2007-07-18 06:19
Location: 35° 50 N, 14 º 35 E
Been thanked: 2 times

Using structs as classes in C without contortions.

#1 Post by edbarx »

Is it possible to use classes in C? I know there is a contorted way by using structures with data fields and function pointer fields, but that, is not enough. The structure's data fields would still be hidden inside the structure forcing coding like this:

Code: Select all

int method_1(structure struct1, ...) {
   struct1->field5 = expression;
}
A call to such a method would look like:

Code: Select all

struct1->method_1(struct1, ...);
In other words is there a neat way of accessing a struct's fields inside a function assigned to a function pointer declared within the struct? The struct1 parameter shouldn't be necessary.

Proper Object Oriented languages permit a method to access any of a class's fields with no extra declarations required.

Can I do the same in C?
Debian == { > 30, 000 packages }; Debian != systemd
The worst infection of all, is a false sense of security!
It is hard to get away from CLI tools.

reinob
Posts: 1189
Joined: 2014-06-30 11:42
Has thanked: 97 times
Been thanked: 47 times

Re: Using structs as classes in C without contortions.

#2 Post by reinob »

@edbarx,

AFAIK there's no way to to "this" (pun intended :) in C.

You could always build an elaborate object system using the preprocessor, but then you could just dump C for C++.

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: Using structs as classes in C without contortions.

#3 Post by tomazzi »

edbarx wrote:Is it possible to use classes in C?
Yes.
A class is a data structure/object with associated methods/functions which can be used with that strucure/object.
edbarx wrote:In other words is there a neat way of accessing a struct's fields inside a function assigned to a function pointer declared within the struct?
No. But there's no real need for this.
The C++ (and a few other languages) are providing the "this" pointer, what can be considered as an advantege in comparison with C, but the truth is, that it's not.
The "this" pointer is passed to the function just like a formal argument - it is just invisible at the syntax level.
So f.e. the following 2 examples are equivalent:

Code: Select all

class int_class {
   int val;
   inline int get_val() {return val};
};

main () {
   int_class value; // yeah no constructors, just an example ;)
   cout << value.get_val();
}

Code: Select all

typedef struct {
   int val;
} int_class;

inline int get_val(int_class* this_pointer) {
   return this_pointer->val;
}

main () {
   int_class value; //yeah, no init/constructor -> uninitialized :)
   printf("%d", get_val(&value) );
}
Regards.
Odi profanum vulgus

User avatar
edbarx
Posts: 5401
Joined: 2007-07-18 06:19
Location: 35° 50 N, 14 º 35 E
Been thanked: 2 times

Re: Using structs as classes in C without contortions.

#4 Post by edbarx »

In other words in C classes are structures containing all the data fields that would be placed inside a class with no visibility specifiers like private, public and published.
Debian == { > 30, 000 packages }; Debian != systemd
The worst infection of all, is a false sense of security!
It is hard to get away from CLI tools.

Funkygoby
Posts: 68
Joined: 2009-08-13 09:25

Re: Using structs as classes in C without contortions.

#5 Post by Funkygoby »

edbarx wrote:In other words in C classes are structures containing all the data fields that would be placed inside a class with no visibility specifiers like private, public and published.
It can also contains function pointers organized (or not) in vtables: http://cecilsunkure.blogspot.fr/2012/08 ... table.html

I believe you can separate private from public variables and methods with some thinking (and readings!!)

You can google "oop c" wich will you give interesting readings. It is apparently considered bad practice to be doing object-oriented programming in c but it helped me understand what it is actually (something my friends studying CS failed to explain to me, maybe they never understood it themselves).
I am an enthousiastic C guy therefor I understand faster if I can see what is happening under the hood.

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: Using structs as classes in C without contortions.

#6 Post by tomazzi »

Funkygoby wrote:
edbarx wrote:In other words in C classes are structures containing all the data fields that would be placed inside a class with no visibility specifiers like private, public and published.
...
I believe you can separate private from public variables and methods with some thinking (and readings!!)
The funny thing is, that today's object programming theory and object-oriented languages are in general nothing more than reinventing the wheel - only the results are less efficient and less reliable, because they depend on high amount of the glue code generated by compilers, which will always have bugs and wchich will always be slower.

The translation unit in C is a class - or at least it should be used like this, but unfortunately many people just don't get it.

For example:
1. Every static declaration in the unit means a "private" field or member function/method, but it can be also used for reference counting. Static members are invisible outside the unit.
2. Every non-static decalaration means "public".
3. Derivation, inheritance and polymorphism was available in C from its very beginning, since any data structure can be build with other data structures and extended by additional fields.
4. The header can be used to implement "protected" interface and "friend" classes by revealing parts of the interface, depending on who is including the header (using preprocessor symbols)

Regards.
Odi profanum vulgus

User avatar
edbarx
Posts: 5401
Joined: 2007-07-18 06:19
Location: 35° 50 N, 14 º 35 E
Been thanked: 2 times

Re: Using structs as classes in C without contortions.

#7 Post by edbarx »

Funkygoby wrote:
edbarx wrote:In other words in C classes are structures containing all the data fields that would be placed inside a class with no visibility specifiers like private, public and published.
It can also contains function pointers organized (or not) in vtables: http://cecilsunkure.blogspot.fr/2012/08 ... table.html
Function pointers in C structures are only a cosmetic construct as there is no "this" or "self" pointer passed to such functions. Using function_name(struct_class_type object1, ...) is enough. The syntax object1.function_name(struct_class_type object1, ...) defies the purpose of defining function pointers to act as class methods.
Debian == { > 30, 000 packages }; Debian != systemd
The worst infection of all, is a false sense of security!
It is hard to get away from CLI tools.

Post Reply