• Advertisement
Sign in to follow this  

Good Reading Material On C++ References And Pointers?

This topic is 1929 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


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

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().

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:



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. :)

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement