Good Reading Material On C++ References And Pointers?

Started by
3 comments, last by slicksk8te 11 years, 6 months ago
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 :)

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

Advertisement
Have you tried searching the web? What was wrong with the material you found doing so? Perhaps you have more specific questions?
-> 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().

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

A Simple trick you can easily remember which has helped me alot recently.

A pointer is:



char * name = "Paul";

cout << name[0];

// Which will print out P.

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

// Will print out Paul.


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.


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

int * score = 500;
int bobScore = &score;


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. :)
Game Engine's WIP Videos - http://www.youtube.com/sicgames88
SIC Games @ GitHub - https://github.com/SICGames?tab=repositories
Simple D2D1 Font Wrapper for D3D11 - https://github.com/SICGames/D2DFontX
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]

This topic is closed to new replies.

Advertisement