Jump to content

View more

Image of the Day

The night is still, but the invasion brings chaos. #screenshotsaturday #hanako #indiegame #gameart #ue4 #samurai https://t.co/cgILXuokoS
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Good Reading Material On C++ References And Pointers?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 superman3275   Members   


Posted 09 October 2012 - 04:37 PM

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 ph34r.png!

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:


or Personal-Message me on here smile.png!

#2 Álvaro   Members   


Posted 09 October 2012 - 07:01 PM

Have you tried searching the web? What was wrong with the material you found doing so? Perhaps you have more specific questions?

#3 EddieV223   Members   


Posted 09 October 2012 - 10:18 PM

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



// 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



// SFML 2 book



#4 SIC Games   Members   


Posted 10 October 2012 - 11:00 PM

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[i];
// 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

#5 slicksk8te   Members   


Posted 11 October 2012 - 10:18 AM

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 xint x = 1;// Declare a pointer variable pxint *px;//Setting px to the address of xpx = &x; // & is the address of any variable// Now get the value that px points to and put it in yint 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 bytesstruct ReallyBigData x;// now if you pass it by valuefoo(x)//Then all of x is being passed to the function, yep all 2000 bytes// if you had thisReallyBigData* px = &x;//Then px is only 4 bytes!// So an alternate function that takes a pointer instead of the struct would bebar(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 xReallyBigData x;// Fill the pointer with the address of xReallyBigData* px = &x;// Manipulate x directlyx.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, 11 October 2012 - 10:42 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.