C vs. C++...

Member
Posts: 100
Joined: 2006.05
Post: #1
Ok this is branching from my other thread. But I would really like to hear some arguments on the issue of C vs. C++. I chose C++ because of some of the features I heard it has over C, but mainly because I just ordered a book on C++. Anyway, I hear a lot about C being standard for game programmers. Why? I heard OOP is a big thing about it...
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2003.02
Post: #2
Thats why i use c++ now. i learned it just for game programming and udg. a class just makes more sense to me than a structure and a buncha functions. plus it seems neater to go model.Draw(); instead of Draw(model);

laziness,impatience, and hubris
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
C++ had a few goals:
1) be backward-compatible with C
2) be object-oriented
3) be efficient

Unfortunately, 2) and 3) are essentially incompatible goals. To get around this problem, C++ has two different "modes" of member function, virtual (inefficient) and non-virtual (efficient). Unfortunately, neither of these methods really provides the flexibility that you'd get in a truly object-oriented language.

To partially alleviate that lack of flexibility, they introduced templates. Now, don't get me wrong, templates are incredibly powerful and you can do a lot of cool stuff with them. They're also incredibly difficult to program with if you go beyond the basics, and produce impossible-to-read compiler errors if you make even a tiny mistake. They fix some of the flexibility issues, but only at compile-time, where the standard OO solution works at run-time too.

Then in order for all this to hang together, they had to introduce a second way of passing objects by reference, which makes it look like you're passing them by value when you're actually not, they had to allow you to overload operators with whatever meaning you want (nothing stopping you overloading + to mean "remove this object from the queue and send a message across the network"), they had to allow implicit conversions between user-defined types, and because there's no garbage collection, they had to allow you to run arbitrary pieces of code whenever your objects get destroyed.

So basically what you've got is C, with a few semantic differences which mean it doesn't behave quite like C, and a bunch of hacks on top to get some degree of object-orientedness.

The end result is, C++ isn't really any good for efficiency, since you can't tell what's going on just by looking at the code, and it's not really any good at object-orientedness, since you can't do everything you really want to for good object-oriented programming.

The solution then is either to find a language which allows you to use C at the low levels and a proper object-oriented language at high levels where efficiency isn't a concern (*cough*Objective C*cough*), or to use any high-level object-oriented language (*cough*Ruby*cough*) and call out to C functions for bits that are too slow.

Whichever you choose, programming the high-level logic will be much easier due to better OO support, and programming the low level will be much easier due to knowing what's going on and therefore being able to see why things are slow.

That's my rant for the day Smile
Quote this message in a reply
Member
Posts: 79
Joined: 2002.08
Post: #4
Quote:Originally posted by OneSadCookie
That's my rant for the day Smile [/b]


Yeah, we all know you don't like C++ and take any opportunity to bash it.

I love C++. And don't say ObjC is any more efficient when it comes to method calls. There's quite a lot going on behind the scenes there too when you call class methods. And you can use straight C and C++ just as well as C and ObjC. And then we have ObjC's horrible syntax...

My rant of the day.

KenD

CodeBlender Software - http://www.codeblender.com
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
Quote:Originally posted by KenD
And don't say ObjC is any more efficient when it comes to method calls.

Oh no. Quite the opposite, much slower than C++. Much more flexible, too.

Quote:And then we have ObjC's horrible syntax...

Objective C has the best syntax of any language I've ever used. Being able to distribute the method name amongst the arguments makes reading code like reading prose. You just don't get the situations you do in C/C++ where you have five or six arguments to a function and you can't figure out what the last two or three mean without looking at the docs...
Quote this message in a reply
Mars_999
Unregistered
 
Post: #6
Quote:Originally posted by OneSadCookie
Oh no. Quite the opposite, much slower than C++. Much more flexible, too.



Objective C has the best syntax of any language I've ever used. Being able to distribute the method name amongst the arguments makes reading code like reading prose. You just don't get the situations you do in C/C++ where you have five or six arguments to a function and you can't figure out what the last two or three mean without looking at the docs...


Muahahh learn to use descriptive words for variables and function parameters! Not this Foo(x,y,z) <- whatís that? I have read some of the top game programmers in the industry say the speed delta between C and C++ is minute.
Quote this message in a reply
Hog
Member
Posts: 151
Joined: 2002.09
Post: #7
Quote:Originally posted by OneSadCookie
...the arguments makes reading code like reading prose. ...

reading prose is hell difficult and requires alot of concentration to figure out the exact meaning. it's awfully fuzzy
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
Yeah, most of the time C and C++ will run at about the same speed (with GCC < 3.3 I sometimes found C++ to be noticeably faster than C).

The point is that if something isn't running fast enough, you know what's going on just by looking at the code in C. In C++ you pretty much have to disassemble it to figure out where all the constructors, destructors, and overloaded operators are coming from and if any of them is your problem.

Objective C is not "fuzzy", nor need prose be. In ObjC, though, most of the words you'd expect to be in an English sentence are present, and each argument to a function call is labeled. You're never left guessing as to what the intent was, or as to what it means to pass 3, false as the last two arguments to createWindow() because it'll be createWindow: level: show:.

Without having been exposed to the API or system before, you can jump right in and understand 99% of what's going on, without having to refer to any headers or documentation. Figuring out what an existing C or C++ system is doing usually means opening up about 100 files tracing function signatures.
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #9
Quote:Originally posted by OneSadCookie
Figuring out what an existing C or C++ system is doing usually means opening up about 100 files tracing function signatures.


and if we like doing that?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
Quote:Originally posted by skyhawk
and if we like doing that?

You're a masochist and I take no responsibility for lost time, money or sanity. Just don't ever try to claim I didn't tell you there was a better way!

(Also note, you can still do this in Objective C, it's just seldom necessary).
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #11
well... I am a masochist because I use Obj-C++... I like the Obj-C system calls and such, but when it comes to making my own classes, I find it easier to use C++ classes. I don't do anything funky with these classes other than using them to encapsulate data and having the functions being a part of the structure to use the data.
Quote this message in a reply
henryj
Unregistered
 
Post: #12
Ruby is the best language, followed by C++. C is for people who people who can't think past 2 source files (or learnt to program in 1965) and OBJ-C is for poofters who can't use real languages.
Quote this message in a reply
Tycho
Unregistered
 
Post: #13
Stolen from the OpenGL Programming Guide:

Quote:C
God's programming language.

C++
The object-oriented programming language of a pagan deity


Not that this forms any real argument, I just like the quote.
Quote this message in a reply
Tycho
Unregistered
 
Post: #14
I think saying any language is "the best" is asking for a flamewar. Let's get some Lisp zealots in here. The arguments will last for years...
Quote this message in a reply
Hog
Member
Posts: 151
Joined: 2002.09
Post: #15
Quote:Originally posted by Tycho
Let's get some Lisp zealots in here. The arguments will last for years...
probably not. it won't last for a few seconds in OSCs forum
Quote this message in a reply
Post Reply