Some basic C++ questions

Started by
10 comments, last by iMalc 18 years, 10 months ago
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.
Advertisement
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.
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.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
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.
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.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
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]

- Jason Astle-Adams

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.
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
I'd recommend you try one of the simpler 2d libraries such as SDL, Allegro or ClanLib.

You can find some links to some good Allegro and SDL tutorials here, otherwise just Google for some.

- Jason Astle-Adams

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.

This topic is closed to new replies.

Advertisement