C++ and Classes

Hello everyone…

I have been C coding for a few years now, and have not gone to C++ because I heard that it is not easily “portable”…

Now that I think of it, why wouldn’t it be? I thought you could use C++ with linux

What I am asking is if anyone can list any advantages and/or disadvantages of using C++ for an OpenGL based engine…and what the “portability” capabilities are…

Thanks for your time,
Drak

Sir I’m sorry but I really don’t understand your question. Are there in the world programmers that code in pure C? I learned about them in the history books. They said that they were good but they don’t exist today. Why? Because today programming is Object orientated and C isn’t object orientated (but somehow you could emulate some classes with struct). The main difference between the C and C++ is the ++,-- operators, the appearance of keyword class, I think that the virtaul tables of the classes, operator overridng and overloading aso

NewROmancer

Thanks for replying!

Well, for example…ignoring the fact that there was some assembly code in it, Quake was written in just C, and i imagine Quake2 was too.

What I want to know is if C++ programs can be “ported” to other platforms easily (e.g. linux). Some people say no…and I want to learn why! because “object-orientism” seems the way to go for really clean code!

Thanks!

Originally posted by NewROmancer:
Sir I’m sorry but I really don’t understand your question. Are there in the world programmers that code in pure C? I learned about them in the history books. They said that they were good but they don’t exist today. Why? Because today programming is Object orientated and C isn’t object orientated (but somehow you could emulate some classes with struct). The main difference between the C and C++ is the ++,-- operators, the appearance of keyword class, I think that the virtaul tables of the classes, operator overridng and overloading aso

Huh???

Yes, there are quite a few programmers still writing pure C, though usually for systems software rather than apps. OO is wonderful for a great many things, but it’s not a silver bullet. (Which is why Stroustrup describes C++ as a “multiparadigm” language rather than an OO one.) Almost every benefit of moving to C++ has an associated downside. Dynamic binding means sacrificing a lot of static validation. STL collections and algorithms mean sacrificing useful compiler errors. Abstracting away from the machine makes it harder to achieve optimal performance (not impossible by any means, but harder).

And yes, there are still some portability issues with C++. The core features should compile just about anywhere these days, but some of the more abstruse features (member templates, Koenig lookup etc) are patchily supported. MSVC isn’t too hot on standards compliance, and I suspect they aren’t going to do much to rectify the situation.

Re your list of differences… ++ and – are in C as well. Maybe you’re thinking of the use of >> and << for stream IO, which is just an application of operator overloading, not a language feature in itself. The real big differences are:

  • classes
  • virtual functions
  • templates
  • exceptions
  • overloading

To answer the original question; yes, I think switching from C to C++ is a net win for application-level programming. But be prepared to put a fair bit of effort into learning it before you see the benefits. I like C++ a lot, and use it pretty much exclusively, but it’s NOT easy, and you can get yourself into a world of trouble if you don’t know what you’re doing. If you’re comfortable with C, as it sounds, expect to see a drop in productivity before you see a gain.

Followup to drakaza’s reply… most of the portability issues are history now. If you look at the Mozilla coding standards from a year or so back, they’re fairly scary - don’t use templates, don’t use exceptions, don’t use namespaces etc etc. But things have improved to the point where you can write XP C++ and be pretty confident it’ll work. Basically, if it builds on gcc and MSVC you should be OK bar the odd tweak here and there.

Oh, and Carmack has said he’ll start using C++ in his next game.

If I recall correctly, someone from Bullfrog said in an interview that the company was beginning to prefer C++ over C.

Thanks Heaps MikeC, that is exactly what i was after…

I actually DID know C++ but never bothered to program with it because i was trying to learn C first… I actually understood it better than C

Now that i am studying JAVA at university, I have a feel for OO, and I see that the cleanness of OO-tism just seems to override any downsides that I can currently see…

so basically the only thing that has to be “ported” is the OS specific code, right?

Thanks very much!

No prob. :slight_smile:

If you’re going from Java to C++, bear in mind that C++ is a much richer language - if you only use the same feature subset that Java does, you’ll be missing out in a big way. Templates are the obvious case; less obvious but equally important are inlined concrete classes. (i.e. classes with no virtual functions).

What I always hated about Java was that you kept ending up with a mess of primitives for small-grained data, because Java’s everything-is-an-Object straitjacket imposes too much space overhead to make small objects acceptable.

For instance, when I was writing my C++ math lib I kept getting my angle units mixed up. (Math uses radians, OGL uses degrees, animation and modelling often use revolutions.) So I wrote a little Angle class, just wrapping a float, so that all get/set methods made the units explicit, e.g. myangle.radians() or myangle.degrees(). Since then, zero unit errors, without any space overhead. You just wouldn’t do that in Java.

Yeah, that sounds like a very good idea, because i get my angles mixed up frequently as well

It looks like a long few weeks ahead of me now!

Thanks!

There is nothing you can do with C++ that you can’t do with C. C++ is not a great OO language it is just designed to make things that require a lot of repetitive work in C easier. Generally speaking, using all the features of C++ is not a good idea: just use the bits that make life easier than C.

On the portability side about half of Microsoft C++ is proprietary, so code is not guaranteed to compile on all platforms unless you stick rigidly to standard C++ (I noticed the NVIDIA devkit uses Microsoft specific C++ however!)

[This message has been edited by foobar (edited 09-14-2000).]

Thanks for posting a sensible reply about some of the differences in C++ and Java MikeC. I go to all these linux websites, not because i use linux but because they got lots of forums on those, and i ask what the difference and pros and cons are of the languages that people know, but these worms just get in fights with each other about whose favorite language has the biggest balls, consequently never returning a non-angry answer. personally i only now pascal (LOL) and C++ because thats what they taught at my high school. I try to learn other languages but get majorly discouraged when i can’t really see the differences in c++ and say perl ( with the exception of a bunch of built in text finder operators booooooring ). anyway, thanks!

foobar - yes, and there’s nothing you can do in C that you can’t do in symbolic assembler, and there’s nothing you can do in assembler that you can’t do in machine code. So? Making life easier for the programmer is a big deal. In practice a competent coder can do things with a HL OO language that a genius coder couldn’t hope to do in asm. Writing a full-featured modern game engine or CAD application in raw hex opcodes may be logically possible, but I’d say it’s not humanly possible. It’s an important distinction.

I agree that trying to use all of C++ is silly; I’ve never seen anybody suggest otherwise, least of all the people who designed it. I think you’re a little harsh though when you say it’s “just” good for automating repetitive C. This is true for vtable dispatching, and partly true for templates, but the thought of trying to implement C++ exception handling in C is enough to give me nightmares. Exceptions are really a whole new programming paradigm.

Not sure what you mean when you say that half of MS C++ is proprietary. AFAIK there’s a few extra keywords (some obsolete), a couple of slightly different implicit casting rules, and not much else. My main beefs with MSVC are 1) the fact that it’s still not 100% ANSI compliant, and 2) the fact you can’t #include the Win32 API header or use the STL without enabling the aforementioned proprietary extensions. Windows I can understand, but not being able to compile the ANSI standard library in ANSI standard mode is contemptible.

grady - yeah, I’ve seen flamefests like that. It’s usually the old horses-for-courses syndrome. Talk to someone doing enterprise server-side programming: C is ancient history, C++ is dead and Java is the Second Coming. Talk to someone doing systems work: asm is reasonable at times, C is the norm and C++ is still too big, too far from the metal and not portable enough. (Java, what’s that?) If you do oodles of text processing you couldn’t live without Perl; if you don’t you can’t understand why anyone would touch Perl with a bargepole. This being an OpenGL board, you’ll probably get mostly pro-C++ sentiment since it’s the best fit for the kind of thing most of us do.

(Having dissed Java already, I should confess to severe envy of its dynamic class loading features. C++ kind of sucks in this area, which makes plugin-based architectures more of a pain than they ought to be.)

Sheesh, I’m waffly tonight. Getting verbose in my old age

Shutting up now…

[This message has been edited by MikeC (edited 09-14-2000).]

Thanks for the input MikeC

I was just afraid of losing compatibility in the migration from C to C++, but now i don’t care, because i prefer cleaner code…

One thing I have noticed with VisualC++ though…i made a tokenizer function and it crashes if i create too many tokenizer structs. There is a problem with the memory allocation (and the debug code points to memory.c [which is part of the windows source code) The thing is…it ONLY crashes if i have the DEBUG libraries linked…but it works fine if the Release libraries are linked…

I can overcome the problem by inserting:

malloc(0);

all alone, at the start of my function

It is damn weird!!

Ok! It seems to me that here all the guys just flamed like little kids. C is very good but C++ is better. The OO programming is good for games. Let me explain why. Here a few weeks ago was a huge topic of Schlornenburg (Sorry if I mispelled your name) about how can he select a ship in his game. The whole problem was because his program I think it isn’t OO. If you want to make a game first you make an abstract class CRealWorldObject from what you derive two abstract classes CRealWorldObjectStatic and CrealWorldObjectDinamic also virtual classes. From CRealWorldObjectStatic you derived the classes for the buildings, trees aso and in the other class you derive the classes for your personages of the game and NPCs. Do this in pure C and you will see that is hell on earth. Doing this class ierarchy makes the logic of the game a lot easier and you can use the polimorphism , late-bindingcode reutilization,virtual functions aso things that will make your work a lot easier and will greatly reduce the time for coding.

This is it all!
NewROmancer

Who’s flaming? I thought everyone was being remarkably civilized for a language advocacy thread

Does anyone know much about the C-sharp language that is worth looking into?
Or is it just simply a little bit easier to use and more flexible than C* (wildcard)

To everyone outside Microsoft PR, C# looks like a cross between C++ and Java. It has a couple of interesting features compared to C++ (GC and a better component model) but it will be Microsoft-only, regardless of what the PR says, so don’t even think about it if portability is a concern.

Thanks MikeC,

It looks like I won’t touch C-sharp with a 10-foot stick if I could help it!

Damn it, they have the non-portable DX, now a whole language that is MS only? JEEZ!

They might as well buy a chunk of the planet

That is, if they have not already…

I’ve found that you have to have a fairly
low-level technical knowledge of how C++ is
implemented to avoid shooting yourself in
the foot, performance-wise. Luckily, I have
acquired that knowledge :slight_smile: Looking at the
assembly generated by different compilers is
quite educational; you should try it.

If you know C, there is no specific reason
to go to C++, other than for educational
purposes.

If you already know C++ and are familiar in
it, by all means use it. It’s as portable as
C these days. Just make sure to not overuse
virtual functions, virtual inheritance, RTTI
or exceptions, as these ARE slower than
“plain” code. Use virtual functions where
you would use a function pointer in C, and
you’ll do fine.

Especially exceptions are VERY expensive.
Typical implementations are called “zero
overhead” because they don’t incur any
overhead when you declare them – but if you
throw an exception, the runtime will
typically load exception resolution tables
from disk, and walk the stack referencing
these tables, so actually resolving the
throw may be 1000 times more expensive than
a goto!