• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
superman3275

Good Reading Material On C++ References And Pointers?

4 posts in this topic

I am looking for some good, free reading material (Articles, books, websites, etc.) on references and pointers in C++. I find myself not understanding why I'm using -> and not . or why I should pass by value and not reference. All reading material on the subject matter is helpful. (I promise you, nothing you link will be below my level :)
0

Share this post


Link to post
Share on other sites
Have you tried searching the web? What was wrong with the material you found doing so? Perhaps you have more specific questions?
0

Share this post


Link to post
Share on other sites
-> is dereference. You know a pointer points to something, this says, get me what this pointer points to.
MyClass *pClass = new MyClass();

(*pClass) this also dereferences the pointer.

In the first case you now have the object at which its pointing and can access the methods and fields like ->MyMethod();
This says get the object pClass points to and class it's MyMethod().

In the second case the same thing is happening except you have to use the . operator now that you have the object like (*pClass).MyMethod();
This says the same thing.

The reason there is different syntax is because the second example the dereferenced object can now be used as a variable. Like so MyClass myClass = (*pClass);
In this case myClass is now a copy of the object that the pointer was pointing to. It is up to you to make sure the pointer is pointing to valid object.

You can also make a reference to the object pointed to like MyClass &myClass = (*pClass);
In this example myClass variable IS the variable pointed to by pClass. And you access it like myClass.MyMethod().
0

Share this post


Link to post
Share on other sites
A Simple trick you can easily remember which has helped me alot recently.

A pointer is:

[code]

char * name = "Paul";

cout << name[0];

// Which will print out P.

for(int i = 0; i < 5; i++ ) {
cout << name[i];
}

// Will print out Paul.
[/code]

So the * means Points To in address. The & means Reference Of in memory. So, if you were to read a binary file you and you wrote it by Pointing To. You would need to read it back by Referencing it. Perhaps this should help.

[code]
* is Pointing to.
& is Obtraining From or retrieving From Memory Address.

int * score = 500;
int bobScore = &score;
[/code]

So bobScore is obtaining the score Pointed To Memory Location which is just integer 500. I may be confusing though....Sorry but I think perhaps it'll help you understand somehow. :)
0

Share this post


Link to post
Share on other sites
Ok, so this topic is one of the most confusing for new programmers and the confusion is not usually 'what' a pointer is but instead 'why' are they useful and 'how' are they used. Also, the whole value vs reference thing gets really tricky if you don't understand how functions are called.

Lets start with the 'what', just to insure we are on the same page. Pointers are a variable like anything else, except that instead of holding a value directly, it holds an address where that value is in memory. For example:
[source lang="cpp"]// Declare a variable x
int x = 1;

// Declare a pointer variable px
int *px;

//Setting px to the address of x
px = &x; // & is the address of any variable

// Now get the value that px points to and put it in y
int y = *px; // * means get value at address stored in px

// Now y has 1[/source]
Stay with me on this because it is important later. Note that px is just another variable that is 32-bits (or 64-bits on x64 systems). Now why does this matter? Because if you had a variable x that was was 2kb (for example) then you could avoid having to pass the whole variable around by using a pointer. Lets see a code example of this:

[source lang="cpp"]struct
{
int x[500];
} ReallyBigData;

//The sizeof(ReallyBigData) is 2000 bytes

struct ReallyBigData x;

// now if you pass it by value
foo(x)
//Then all of x is being passed to the function, yep all 2000 bytes

// if you had this
ReallyBigData* px = &x;
//Then px is only 4 bytes!

// So an alternate function that takes a pointer instead of the struct would be
bar(px);
// Now it only passes the pointer to the data, Only 4 bytes![/source]

This basicly means that if you have a variable that is large, then it is better to pass a pointer (pass by reference) rather than passing the entire value (pass by value).
This is why you always us char* for strings. Example of functions foo and bar are below:
[source lang="cpp"]void foo(ReallyBigStruct x)
{
if( x.x[0] == 1)
{
printf("It is one!\n");
}
}

void bar(ReallyBigStruct *px)
{
if( px->x[0] == 1)
{
printf("It is one!\n");
}
}[/source]


The difference between '->' and '.' is that it differentiates the type of variable access your using. For example:
[source lang="cpp"]// Declare x
ReallyBigData x;

// Fill the pointer with the address of x
ReallyBigData* px = &x;

// Manipulate x directly
x.x[0] = 1;

// Manipulate x indirectly through a reference (px)
px->x[0] = 1;

// Alternatively you could us px just like x by dereferencing it
(*px).x[0] = 1; // remember (*px) gets the value that px is pointing to
[/source] Edited by slicksk8te
0

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  
Followers 0