Translating c examples to c++ as I go along...

Started by
14 comments, last by daviangel 14 years, 9 months ago
Hey guys, My summer vacation just started, and I figure this would be a great time to teach myself some new things. I've found this book "Game Programming All in One, 2nd Edition" which teaches the reader to build up a 2D game engine in C. I'm not entirely new to coding, and have made a game using a pre-existing engine from school, but this was coded in C++. My question is: are the similarities between c and c++ big enough that I can read the book to grasp the basic design concepts and then translate that to c++ code, or would it be smarter to teach myself c syntax first? (this would probably look good on a resume and it would potentially make understanding code from libraries easier). Also, any suggestions for books that teach the reader to build up a (2D) game engine using c++ would be very welcome.
Advertisement
Since C++ merely extends C, every valid C program is also a valid C++ program.
So if you can read and write C++ code you should have no problem at all grasping C code, you don't have to "teach yourself c syntax".
(There are a few subtle differences.)

But you might be better off picking up a book that uses C++ and an OO approach instead.
I guess I can't really recommend you a book since most of my books are in german.^^
Quote:Original post by DraganO
Since C++ merely extends C, every valid C program is also a valid C++ program.


Incorrect.
Quote:Original post by DraganO
Since C++ merely extends C, every most valid C programs is also a valid C++ program.


Example:
int main (int argc, char *argv[]) {    int foo [argc];    return 0;}

C++ 2003 doesn't know variable length arrays, C99 does.

And this I leave to you as an exercise:
int main (int argc, char *argv[]) {    int template;    return 0;}


For starters, see this.
Quote:Original post by picklejuice
My question is: are the similarities between c and c++ big enough that I can read the book to grasp the basic design concepts and then translate that to c++ code, or would it be smarter to teach myself c syntax first?


When writing code, the knowledge related to the business area (APIs, idioms, strategies, designs, specific algorithms) represents about 90% of what you need to know. The generic knowledge related to the language itself represents about 10%, of which 1% is the syntax of the language itself.

Learning a book like that will teach you how to write games, how to write games in C, and how to write programs in C, at various levels of knowledge. Some of that knowledge (mostly, how to write games) will translate fully to other languages, including C++, while other parts will translate incompletely (how to write games and programs in C) because the languages are superficially similar.

C and C++ are different languages, the mindset and approach to using each is wholly different from the other. C is about allocating values and applying procedures to those values (to intialize, process and uninitialize them), while C++ tends to make a lot of those processes implicit and therefore involves idioms that look, feel and react differently.

Quote:Also, any suggestions for books that teach the reader to build up a (2D) game engine using c++ would be very welcome.
Programming an engine is harder than building a game, in the same way that programming an accounting program is harder than being an accountant, and programming a program to do X is harder than doing X—not only do you need to know what doing X involves, you also have to think about creating a program that does it.

Until you know what developing a game involves, you will have trouble understanding what an engine should do. And knowing what developing a game involves, sadly, requires you to develop a game first and learn firsthand from the experience.

Quote:Original quite by Cromulent
Incorrect.
Actually, his post is correct, albeit poorly worded. You just misquoted him.

Quote:Original post by ToohrVyk
Quote:Original quite by Cromulent
Incorrect.
Actually, his post is correct, albeit poorly worded. You just misquoted him.


No his point was incorrect.

Not every valid C program is a valid C++ program. I also fail to see how you can misquote that as the context of his post was consistent.
Quote:Original post by ToohrVyk
Quote:Original quite by Cromulent
Incorrect.
Actually, his post is correct, albeit poorly worded. You just misquoted him.


I checked DraganO's post again. Without being a logician, his post reads like "Every valid C is also valid C++. Not every valid C is also valid C++". Hence I think the content of Cromulents post was really undefined, because a paradoxon can't be incorrect, but it also cannot be correct.



Quote:Original post by Cromulent
Quote:Original post by ToohrVyk
Quote:Original quite by Cromulent
Incorrect.
Actually, his post is correct, albeit poorly worded. You just misquoted him.


No his point was incorrect.

Not every valid C program is a valid C++ program. I also fail to see how you can misquote that as the context of his post was consistent.


What ToohrVyk wanted to say is that the block you misquoted from DraganO had a postfix of "(There are a few subtle differences.)", which you have oversighted.
The essence of his post was "With a few minor changes, every valid C program is also a valid C++ program". Can you think of any major changes that would need to be applied to a C89 program (small syntax changes, and variable renaming, are minor) for it to be compiled by a standard C++ compiler?

I will admit that the post was poorly worded, since the "With a few minor changes" only appeared two sentences later as the "There are a few subtle differences" parenthesis. However, the document he knowingly linked to (and phresnel also linked to later in the thread) is in direct contradiction with the unmitigiated assertion that "Every valid C program is also a valid C++ program" and I can only see two possible explanations:

  1. DraganO is stupid enough to link to a reputable document that directly and fully contradicts what he is trying to say, or
  2. DraganO intended the wikipedia reference to act as a "With a few minor changes" clause in his message, but messed up the wording and it did not come out right.


Out of respect for my fellow forum members, I will choose the explanation that assumes the least stupidity on their parts—DraganO may feel free to prove me wrong [smile]
Oh, come on guys...
Okay, I admit that my choice of words was a bit inauspicious.
What I meant to say is that a valid C program is also (at least most of the time) a valid C program, since the OP was about if it is possible to read and understand C code with C++ knowlege, which most certainly is the case.
I did add the link to point out that there are differences, but they are irrelevant if he doesn't want to compile C code as C++ code, but only read it.

Quote:Out of respect for my fellow forum members, I will choose the explanation that assumes the least stupidity on their parts—DraganO may feel free to prove me wrong

Actually I am quite known to behave stupid frequently, so you won't have to wait long for proof.^^


Parkinson's Law of Triviality
Quote:Original post by DraganO
Oh, come on guys...
Okay, I admit that my choice of words was a bit inauspicious.
What I meant to say is that a valid C program is also (at least most of the time) a valid C program

But, but, a valid C program is always a valid C program *ducks and covers*

This topic is closed to new replies.

Advertisement