do i have a use for pointers?

Started by
38 comments, last by ToohrVyk 18 years, 3 months ago
Okay, my question is this. What do I need pointers for? I have a program now (and several ive made before) and i've never used a pointer once. Basically I found them over complicated when i could just pass in a value instead of a pointer to a value. I heard they save space because the function the pointer is passed to doesn't need to duplicate the whole variable so i decided it'd be in good form to start using them. But when and where do i use them? I've not found a place. Basically I have a function right now that is in a class called CButton. It's a function that checks the state of the button and then sets a integer called state within the function to a different number depending if the mouse is clicking, hovering or not on a button. I know for other functions (ie. my SetButton function) i dont need pointers cuz the values go like so. void SetButton(char name[], int x, int y, int r, int g, int b); and for each one i give a constant value like this button_new.SetButton("New Map", 10, 10, 150, 90, 90); but for my function CheckState, do i use pointers? should i declare it like this CheckState(int x, int y, bool click); or with pointers like this CheckState(int *x, int *y, bool click); or is using pointers for small variables and data like that just gonna get mucky and over complicated? Is using them for integers really saving as much space as i've read to be true? Or should I use pointers for functions that use bigger data types...like bitmaps? ie: void DrawBitmap(BITMAP *bm); Even then i'm not sure i should, cuz in my other program i have functions i pass bitmaps to and it's not slowed down or anything because of that. Help please.
Advertisement
Later on in game programming when you have to "pass" an entire surface to a function, instead of creating an entire copy of it the function edits the one passed to it which potentially saves a huge ammount of memory (which you already knew). So another case where you would be required to use a pointer is if you want the function to edit the variable instead of just editing the temporary variable created for the called function. Hope that makes sense to you, ask me to clarify if it doesn't.

EDIT: An example
#include <iostream>using namespace std;void SomeFunction(int Var1);int main(){  int Var1 = 3;  cout << Var1 << endl; // What will this print?  SomeFunction(Var1);  cout << Var1 << endl; // And what will this print?  return 0;};void SomeFunction(int Var1){  Var1 = 5; // Edit the value  return;};

Now using pointers make SomeFunction change Var1 in main to 5.
What we do in life... Echoes in eternity
in windows or sdl or any higher level programming pointers are absolutley needed.. i once thought exactly what you did but you jump into sdl or windows api and ull quickley learn how much they are needed.. the only time you should really use them is if your wanting to edit it... now like this..

void timestwo(int x)
{
x = x * 2;
}

x dies and nothing happens but if you do

void timestwo(int *x)
{
&x = &x * 2;
}


not sure about syntax.. but there youve just directly edited the data in the pointer... hope this helped you
Use references where you can, pointers where you have to (note that this covers C as well, since you "have to" use pointers for all reference uses there). You want to pass a reference or pointer to a function when:

a) the thing being passed is large enough to benefit - as a rule of thumb, use it for structs and classes, and pass primitive types by value. (Use a const reference in this case, unless (b) also applies.)
b) you want the function to change the parameter and for the change to be "seen" by the calling code. (BUT you should first consider if you can just use the return value instead.)

Now then:

- Please don't put that C in front of your class names. Similarly, don't add 'Button' to the names of Button member functions. Neither of these adds useful information for either you or the compiler.

- Use std::string for text data. It will save you headaches in the long run.

- Since this class represents a "button" of some sort, check that your API doesn't already provide something similar.
Jembulura: The first part about passing a whole surface and saving memory etc. makes sense but the second part about passing a pointer to edit a variable outside the function instead of the temporary one created doesn't. Why not just return the new value and set the variable that way?

Also, should I use pointers for my CheckState function or what that be too much of a hassle for the small amount of space it saves, besides i'm not aiming to change the mouse's position; just to read it.
for preformance sake really... and energy sake for you.. lol why add all the time into adding more lines of code.. take memory up because its editing a deplicate.. send it back and the memory still is wasted... when you can use once chunk of memory and keep it simple?
if the your classses are potentially large( contains a std::vector, or some cynamic array ), then you shouldnt pass by value. this means passing using pointers, which is syntactically difficult, or refernces, which are easier on everyone.

//so instead ofvoid SomeFunction( PotentiallyLargeType value );//we could usevoid SomeFunction( PotentiallyLargeType& reference );//orvoid SomeFunction( const PotentiallyLargeType& reference );


the const means that you promise the compiler you wont change the object, just inspect it...

pointers do the same thing as references but are more difficult, but they can do more.
About the only place I can think of were you absolutely need to use a pointer is dynamic memory allocation.

That of course begs the question as to when you would use a reference. A few have already been mentioned, polymorphism would be the biggest one.
[size=2]
Quote:Original post by Le_Danse_Macabre
Jembulura: The first part about passing a whole surface and saving memory etc. makes sense but the second part about passing a pointer to edit a variable outside the function instead of the temporary one created doesn't. Why not just return the new value and set the variable that way?

Also, should I use pointers for my CheckState function or what that be too much of a hassle for the small amount of space it saves, besides i'm not aiming to change the mouse's position; just to read it.


pointers can be used to "return" several values at once.

e.g.

void whereIsMouse( int& x, int& y );

if the time taken to copy an int is the slowdown in your program you are not doing too bad at all...

checkState should return the state. its the easiest on everyone, i think
I think i've got it, thanks everyone. Now just for my own satisfaction, i believe this is what i do with the source posted by Jemburula.

#include <iostream>using namespace std;void SomeFunction(int *Var1);int main(){  int Var1 = 3;  cout << Var1 << endl; // What will this print?  SomeFunction(*Var1);  cout << Var1 << endl; // And what will this print?  return 0;};void SomeFunction(int *Var1){  &Var1 = 5; // Edit the value  return;};

This topic is closed to new replies.

Advertisement