Sign in to follow this  
gav86

C++ question(s)

Recommended Posts

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!

Share this post


Link to post
Share on other sites
The '**' is usually used to declare a pointer that will be used as a pointer to an array of pointers.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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 say

foo 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.

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