# 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 on other sites
The '**' is usually used to declare a pointer that will be used as a pointer to an array of pointers.

##### 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 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 on other sites
Quote:
 Original post by EndarThe '**' 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 on other sites
Quote:
 Original post by Boku SanOT 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 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 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.

##### Share on other sites
ah! Thankyou!

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

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627667
• Total Posts
2978533

• 9
• 10
• 10
• 12
• 22
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.20205068588256836, "perplexity": 2660.778491355163}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2017-43/segments/1508187823260.52/warc/CC-MAIN-20171019084246-20171019104246-00340.warc.gz"}