Sign in to follow this  
hiroshisan

Some basic C++ questions

Recommended Posts

hiroshisan    112
I know that the way C++ works is second nature to some, but I noticed some differences (apart from syntax) where C++ differs from JS and WDL. I was just hoping someone could clarify these for me. Namely, in most of the languages I've used, a function must be defined before it is called. Meaning that if ragnarok() is called from main(), ragnarok() must be defined above main(). However, in all of the references (namely "Teach Yourself C++ in 21 Days"), a function prototype is put above the main function, but the function itself is defined below it. This seems like a discrepancy to me. Can you define a function above main() as well? That was just bothering me. Come to think of it, that was the only issue I had. Apart from that, all I was looking to know is, does anyone know any good references to help learn Windows programming. For making games, I mean. Because right now, I'm working in DOS, but I want to work my way up. You know how it is. DOS...ech.

Share this post


Link to post
Share on other sites
choffstein    1090
Yes, you can define it above Main. If you think of it from a compiler's point of view, the compiler just simply needs to make sure the function exists and is being called with the right parameter types, etc. The compiler doesn't need to know how the function works until it gets around to actually compiling its code.

So, in short, yes, you can either define it above or put the prototype above.

As for beginning with Windows programming -- look at the tutorials on this site. They are an often overlooked, brilliant reference.

Share this post


Link to post
Share on other sites
jfclavette    1058
Quote:
Original post by hiroshisan
Can you define a function above main() as well? That was just bothering me.


Yes you can, but eventually you'll just end up with multiple files anyway. It can also happens that A calls B and B calls A, which one would you put on the top then ? Better get used to it now.

Quote:
Original post by hiroshisan
Come to think of it, that was the only issue I had. Apart from that, all I was looking to know is, does anyone know any good references to help learn Windows programming. For making games, I mean. Because right now, I'm working in DOS, but I want to work my way up. You know how it is. DOS...ech.


Personally, I wouldn't advise you to learn any Windows programming in C++ with the current state of affairs. A lot of Windows API are being redesigned and much of them are quickly becoming obsolete. If you want to learn Windows programming, I suggest picking a language such as C# (Managed C++ could also be an option, but I for one really didn't like it). If you only want to make games, look into libraries like SDL, which not only have the advantage of being cross-platform but are also much easier to use.

Share this post


Link to post
Share on other sites
silmaril    175
An identifier like a function must be declared before it can be used. That is what the prototypes are. It doesn't have to be defined, e.g using a function body, to be used, but must be provided during linking if it is a staticly linked. If it's put under the main() function it will be in the same translation unit and linked in. You could also have provided the function definition in another file and linked them together.

Note that a definition of a function is a also a declaration, so if you did define it above main() you wouldn't need a separate declaration.

Share this post


Link to post
Share on other sites
deadimp    310
And to go a step further, if you were to declare a variable in a separate object file (which would be a compiled *.cpp), and you want to access it, you would have to tell the compiler that it exists also. To do this, you use the "extern" keyword. Example:
<Bob.cpp>
bool BobEatsCheese=true;

<George.cpp>
extern bool BobEatsCheese; //Tell the compiler it is there somewhere.

NOTE: If you use the "extern" keyword and call it in your code, yet it is not in an object file, a linker error will be returned.[/code]

Share this post


Link to post
Share on other sites
jbadams    25676
Also, just to add one more note on the matter, a lot of compilers will accept code where you have a function prototype but no definition later in the code. It'll compile, but it sure won't run properly - keep an eye out for that one, I tripped myself up once by putting in all the prototypes first and then forgetting one of the functions. [embarrass]

Share this post


Link to post
Share on other sites
hiroshisan    112
I think I get it. Defining the function is kind of flexible as to location because, since it is being compiled, it will be found one way or another?

And is Windows programming a bad idea at the moment? I wanted to get into sprite-based stuff (side scrollers, maybe overhead shooters, or even something more simple than that). Anybody know any tutorials for that kind of thing. Even just knowing how to make tetris would be a start. I mean I'd love to do 3D stuff, but I can't even imagine at this point how I would even get the code to interpret anything I've made in 3D Studio Max.

So are there any comprehensive tutorials on sprite-based gaming anywhere. Not necessarily for Windows, just as long as it's not DOS. I'm sick of DOS giving me that monochrome stare. It creeps me out.

Share this post


Link to post
Share on other sites
DanLatimer    139
you can also make inline functions in c++ if you want too, they are always fun. with the definition inside the prototype. fun times! :):)
lol and if you dont like all the non color fun of dos, use msdos! hahah the command "color" makes every command saturated in colory goodness! (i like color 0a) black background with green typing... MATRIXIFIED!!! hehehe

Share this post


Link to post
Share on other sites
hiroshisan    112
I'm actually just downloading the newest version of Allegro, because I went today and got a book...Game Programming All-in-One, 2E. It seems like pretty old software, but the principles should be about the same. I think.

Share this post


Link to post
Share on other sites
Zahlman    1682
My policy is to order functions so as to minimize the number of forward declarations that are needed - who wants to have to maintain two copies of the function signature unless it's absolutely necessary?

Share this post


Link to post
Share on other sites
iMalc    2466
In the D programming language you can define functions in any order you like without having to declare them earlier.
[smile]Yay D![smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this