C++ question(s)

Started by
6 comments, last by gav86 18 years, 9 months ago
Hi, I'm totally new to game programming. .. I know some C++, and about the same of java.. . its hard to describe exactly where i am but i suppose things like pointers/references are the most advanced things ive had to deal with. Anyhow recently i've picked up two books - "Beginning DirectX 9" & "3d Game Engine Programming" (Aimed at C++ if you're not aware) - I've seen a declaration like the following in both of them: sometype **pWhatever; I'm just wondering about the ** bit means - does this simply mean "a pointer to a pointer"? This is the only logical guess I can make at it as i've never encountered the double stars before. Also - I don't know alot about things like "#define", "typedef struct" - actually I guess i don't know anything at all at the moment about them - are there any tutorials on these sort of things? All the beginner c++ tutorials are about general programming (conditional statements/loops, classes & OOP, etc) Thanks in advance!
Advertisement
The '**' is usually used to declare a pointer that will be used as a pointer to an array of pointers.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Yeah, ** does in fact mean a "pointer-to-a-pointer" in that, if a variable** is dereferenced, it will still be a pointer. Thus, if you want to call a member function from a CClass** you'd have to do (*yourClass)->function(..)

#define (macro name) (stuff) is a pre-compiler command (marco); essentially the compiler will go through and replace any instances of that macro with stuff:
#define OMG 4 // will replace every OMG with 4.

This can come in handy, but is often frowned upon because you can easily mess things up with more complex macros.

A typedef is essentially an alias for something, be it an object or pointer type or whatever. If you do, say
typedef struct CClass_ {     // ...} CClass;

CClass_ is the actual name of the class, but is interchangable with CClass. I never really saw the point of doing this, but I'm sure there's a valid reason behind it.

(if someone says otherwise than the above, listen to them because I'm sleepy and stupid)
OT a bit, but you've got a really nice book choice with the 3D Game Engine Programming. Kinda strange seeing it side-by-side with "Beginning ...", but...whatever.

SuperPig agrees.
Things change.
Quote:Original post by Endar
The '**' is usually used to declare a pointer that will be used as a pointer to an array of pointers.


Sometimes this is the case, but generally ** is just a pointer to a pointer. So yes, you are correct OP. It comes in handy when passing around objects, although you could probably argue that references would work just as well.

#define is a preprocessor directive. The preprocessor executes at compile-time, as compared to run-time. #define can be used to declare a macro or constant, although there are only special cases where a #define should be used instead of const for constants in C++.

typedef declares a new fixed type in the language. You may use typedef to create pointer types of base objects (ie BASECLASS* to PBASECLASS) or simplify more complicated data types (ie unsigned char to uchar).
....[size="1"]Brent Gunning
Quote:Original post by Boku San
OT a bit, but you've got a really nice book choice with the 3D Game Engine Programming. Kinda strange seeing it side-by-side with "Beginning ...", but...whatever.

SuperPig agrees.


If you meant it seems strange that I bothered to get both of these books - I picked up beginning directX 9 before I had seen this one on game engine programming.

- And yes, from what I can gather, this engine programming book is awesome. . exactly what I was after!

Thanks to the others for the replies to my actual questions. . . I'm sure therell be many more to come!
Note: The phrase "typedef struct" usually refers to the "typedef struct idiom", which is a hold-over from C. It is unnecessary and discouraged in C++, although typedefs in general are still extremely useful (and encouraged) in C++.

Rationale: Suppose you have a struct, and then create an instance of it:

struct foo {  int x;}foo bar;


In C++, this is entirely legal (note that in C++, structs and classes are equivalent - they result in different defaults for public vs. private, but those are easily overridden). However, in C, the instantiation will cause an error, because in C's parsing rules, "foo" is not a type - rather, "struct foo" is. (Of course, a "class foo" wouldn't work at all in C, but that's another story). So you would have to write it "struct foo bar;". This gets annoying and unwieldy, so one would normally use a typedef instead.

One feature of the C grammar, however (which is preserved in C++ only for backwards compatibilty), is that you can declare the struct and do the typedeffing in one fell swoop:

typedef struct foo_ {  int x;} foo; // now "foo" is a typedef for "struct foo_", and we can go on to sayfoo bar;


But again, no reason to do this in C++, so don't! Use the tools you're given, and KISS.

Google finds this reference for me on typedef's, which looks reasonably good to me.
ah! Thankyou!

Who would've thought.. a forum that's actually helpful :D

This topic is closed to new replies.

Advertisement