View Full Version : C++ Pointers
SMASH
June 6th, 2009, 11:07 PM
So I've taken Java classes and learning the basics of C++ on my own was a breeze. Though I finally hit pointers and I understand how it works. I just don't understand why it's practical or why you would ever use it. Can anyone give me an example of it in useful code thats not too complex?
flibitijibibo
June 6th, 2009, 11:13 PM
Pointers can be helpful as a means of backing up data. Let's say we have an instance of some important piece of data, under variable x. If we have a variable y that points to the same instance as x, we can make x a new instance without the original data being lost.
//First, making thems variables.
superImportantStuff x = new superImportantStuff();
superImportantStuff y = x;
//Oh hey, let's try doing something with superImportantStuff.
x = new superImportantStuff(x.getStuff());
x.fuckStuffUp();
//Do we need the backup? Well, let's use our backup.
if (x.isFucked())
x = y;A small, but good basic use of pointers.
SMASH
June 6th, 2009, 11:15 PM
I'd rather it C++ cause thats what I'm teaching myself but I guess the technique is still the same in both.
EDIT: I never got to pointers in Java so actually C++ code I'd be able to recognize better. I guess the real question is why would you use pointers rather than just a variable... I might not even fully grasp the concept of what their used for...
Limited
June 7th, 2009, 11:06 AM
Pointers are a HUGE thing in C++. Its one of the reasons it is so fast.
Why use them? One reason is because you can pass the reference to the original variable, instead of actually passing the variable.
So for instance.
Player *player = new Player("John, 20, "UK", "M");
My player constructor contains the persons name, age, location and gender (M/F).
Now say I wanted a method that returned the name of the player.
public String getName (Player *playerIn)
{
playerIn->name;
}
Now good programming, you should call a method to get that variable, but for ease I've made the variable public so it can be easily read/written to.
Ok heres the important part, when I execute that method.
getName(&player);
I'm passing the memory reference of the player. In regular C++, if you use standard variables, when I pass whatever it is through the method, it creates a local copy in the method. These will be entirely seperate from the copies in the main. So your not actually affecting them, until you do like "return <variable>".
Now for my example, that wouldnt have a huge affect on the speed of the program. However for games programming, say I have a 100x100 grid stored in a vector, every time I pass the vector through the method, it creates a copy. If that vector is like 50mb, you're now playing with 100mb a frame.
It is a hard concept to grasp as the reasoning behind it all doesnt make logical sense (dereferencing usings the same symbol to create the pointer (*)).
But if you want to become efficient at C++ you have to know it.
If you need more examples just say, this is just typed up quick.
Hope it helps.
SMASH
June 7th, 2009, 02:09 PM
Wow, yea, ok I see how it is advantageous to use it when dealing with lists of things, but would you ever use it for just ints or numbers? Or are they just using that in the tutorials to illustrate how it's used...
Kornman00
June 7th, 2009, 09:48 PM
Another thing you may wish to learn are C++ references. Almost like pointers, but without some of the heartache
// you forgot the closing quote on your name ;p
Player& player_heap = new Player("John", 20, "UK", "M");
Player player_stack = Player("Jane", 21, "UK", "F");
public String getName (Player& playerIn)
{
playerIn.name;
}
String s = getName(player_heap);
s = getName(player_stack);
Compiler will implicitly pass the [player_stack] variable by reference (so its memory address on the stack), rather than by value. [player_heap] is declared as a reference only, and its referring to the memory allocated by the "new Player()" (typically on the heap), so no implicit operations are done.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.