Page 1 of 1

classes in C?

Posted: Tue Apr 01, 2003 12:00 am
by OSNewbie
hi i use djgpp to compile C code, and i am wondering if i can create classes in the C language, or do i need to use C++ to do this. if i have to use C++, are there any problems with using that to code an os?

RE:classes in C?

Posted: Wed Apr 02, 2003 12:00 am
by common
No, C does not support classes.  However, you have several options still.  You could use objective-C (which supports smalltalk-like OO).  You could also use structs instead of classes.  

If you use C++, you can expect that your constructors/deconstructors may not work inside your classes.

RE:classes in C?

Posted: Wed Apr 02, 2003 12:00 am
by VoidLogic
I have been using mostly C++ in my OS with no problems

RE:classes in C?

Posted: Thu Apr 03, 2003 12:00 am
by common
One may or may not.  It all depends on how your C++ compiler organizes the code in the end result.

RE:classes in C?

Posted: Thu Apr 03, 2003 12:00 am
by VoidLogic
If you are worried about speed, but want the abilty to use classes, you can always write your code, compile, decompile to asm and do some fine tuning. Watcom works well becuase you can write 32-bit DOS4G PMODE exes. And there are a large number of fast 32-bit graphics, keyboard, mouse and sound librarys for that platform.

RE:classes in C?

Posted: Thu Apr 03, 2003 12:00 am
by carbonBased
C, by definition, doesn't support classes, or any object oriented features (C being a proceedural language).

It is possible, however, with some clevar macros, to simulate a C++ style class interface in C, and emulate many object oriented features.

While working on the latest version of my graphics library I decided to go this route, and have implemented classes with inheritance, overloading and polymorphism and "pseudo-private" variables, macro's for new and delete, as well as try, catch and finally.

It's not perfect.  Polymorphism requires more work on the programmers part (although I'm working on making this even easier).  Inheritance is easy enough to define, but requires typecasting to access inherited values (or use of the "parent" item (ie, parent.inheritedVariable)).

If you're looking for a complete object oriented system, you'd probably be best to use a system designed for it (C++, Eiffel, SmallTalk, Objective-C, etc), but this system can work and from a users stand-point, presents a much nicer API... and from a programmers standpoint, none of the C++ name mangling has to be taken into consideration (which is pain for OSDev'ing, I find).

As an example:

Window *win = Create(Window);
Button *btn = Create(Button);
GUIPart *whatever = Create(ScrollBar);

buttonSetText(btn, "Something");
// or... guiPartSetText(btn, "Something");

guiPartSetColor(whatever, Blue);
// or... scrollBarSetColor(whatever, Blue);

scrollBarSetPosition(whatever, 90);

try {
  windowAdd(win, btn);
  windowAdd(win, whatever);
} catch {
  // do something
} finally {
  // something else
}

If any of this interests you, let me know, and I can see if I can make a little source archive (right now, my source is currently in development, and hasn't been released).

Cheers,
Jeff

RE:classes in C?

Posted: Thu Apr 03, 2003 12:00 am
by OSNewbie
that sounds like a neat approach. i would definetly be interested in viewing some source, if it is complete, but i doubt i would be able to use any of it because i am still learning C... but yes i am interested in seeing it. let me know if you release a source archive for your os.

RE:classes in C?

Posted: Fri Apr 04, 2003 12:00 am
by Tim Robinson
Which constructors are you referring to? Constructors for instances on the stack (local variables) and on the heap (new operator) work OK. For static (global) instances, you have to add a couple of lines of code to call the constructor functions the compiler generates.

RE:classes in C?

Posted: Fri Apr 04, 2003 12:00 am
by carbonBased
It's actually not too difficult to understand, even for the C beginner.  Most everything is accomplished through macro's, and on the implementation side, all that's required is a couple of includes:

#include "object.h"
#include "trycatch.h"

and then simply use the features.

The most difficult part is writting new classes, and even that part is simple enough, just sometimes tedious.

Anyway, I'll see if I can get a source archive of those features, and I'll post a link up on this site.

Jeff

RE:classes in C?

Posted: Sun Apr 06, 2003 11:00 pm
by VoidLogic
Why not just use C++? i am

RE:classes in C?

Posted: Sun Apr 06, 2003 11:00 pm
by carbonBased
> Why not just use C++? i am

I do use C++, for some projects.  But in this case, I'm developing a graphics library, and I want to keep my user base pretty open.  If I develop in C++, I lose a large base of C programmers.

Besides, graphics are low level, and should be optimized to be as fast as possible.  I find C _MUCH_ easier to optimize then C++.  I can guess (and am usually 90% right) at what code the compiler's going to generate from my C statements... with C++, it's a crap-shoot (especially when jumping from compiler to compiler).

Also, when I first learnt C++, so many years ago, there was no standard, STL didn't exist, and a C++ program was MUCH larger then the equivalent C program.  As such, I'm often wary of C++.  I know it's advanced quite a fair bit, but... you know what they say about first impressions :)

Besides, if I want to develop in an object oriented language, I'd rather use Eiffel.  C++ looks gross, as far as I'm concerned.  It's so convoluted, and difficult to read... so many special symbols and subtle nuances.  C's simple, and basic, and I like that.  I've developed enough C routines over the past decade to facilitate most of the advances C++ offers anyway.

And lastly, for OS development, I simply don't like C++.  There are a lot of extra steps, and things to keep in mind when developing an OS in C++, and heaven forbid if you want to mix some assembly in there (or any other language, for that matter); name mangling sucks!

I have nothing against people writting OSs in C++, I think that's cool.  If you can get a nice intuitive API set up in C++, then there's all the more reason to develop in it.  For myself, however, I grew up with proceedural, and low level languages, and C++ just doesn't suit my likes.  I do, however, use it, and I think STL is an excellent addition... although, I think it could've been written better.  Eiffel's implementation is much more intuitive, and simply looks, and acts, better, in my opinion (not that eiffel's perfect either... I just like it better... and no, I wouldn't write an OS in eiffel.  There's a crap load of run-time support to rewrite, and to keep in mind at all time... I still prefer C and assembly for OS development).

Jeff

RE:classes in C?

Posted: Mon Apr 07, 2003 11:00 pm
by VoidLogic
I'll have do disagree, its a matter of opinion. I use ASM for setting up hardware and low level interface; I use C++ for the kernel, which is monolithic and i use C for various areas of mid level absrtaction, like graphics. I guess my case may be different, since my OS, like Win9x, runs on top of DOS in pmode (i use DOS/4G).