do i have a use for pointers?

Started by
38 comments, last by ToohrVyk 18 years, 4 months ago
Quote:Original post by Le_Danse_Macabre
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?


Hey,

If you're wanting to pass back several values, there's no current way to do it outside of creating your own structure to return them, or using a boost? tuple, which _will_ eventually become part of the language the way things are going.

But references/pointers are incredibly handy for the following situations:
int GetMousePos(int* x, int* y)

where you want the user to specify either null [0] or an int which receives the mouse x and y pos. Actually defining and copying from a custom structure here is commonly used, but kind of silly IMO, as this is more straightforward, logical and lightweight.

However, if you didn't want to allow null parameters, and want the function to return two values, both of which are important, use references.
int GetMousePos(int& x, int& y)

Everyone [lots of programmers] make a big kerfuffle about pointers and ignore references, but references generally lead to better, cleaner code, because you are guaranteed to be able to view and edit the variables passed, and the compiler won't allow you to do many silly things with references.

Hope this helps,

CJM
Advertisement
sorry, just interchange the * and the &

#include <iostream>using namespace std;void SomeFunction(int *Var1);int main(){  int Var1 = 3;  cout << Var1 << endl; // What will this print?  SomeFunction(&Var1); // change  cout << Var1 << endl; // And what will this print?  return 0;};void SomeFunction(int *Var1){  *Var1 = 5; // change  return;};
Quote:Original post by Le_Danse_Macabre
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.

*** Source Snippet Removed ***


Ignoring the incorrect syntax..

No. There's no reason to use a pointer over a reference here.

void SomeFunction(int& Var1){  Var1 = 5; // Edit the value};
[size=2]
e.e; sorry everybody, just a few more questions.


* = the value of the variable the pointer is pointing to.

& = the address of the variable the pointer is pointing to.

That's right, right?

And a thing about my CheckState function. I'm not using the function to obtain the values of the mouse x/y or to return them, in fact i'm just using it to check them. like so:

void CButton::CheckState(volatile int *mx, volatile int *my, bool clicking)
{
if((*mx > x) && (*mx < x+50))
{
if((*my > y) && (*my < y+15))
{
if(clicking)
state = 2; // clicked
if(!clicking)
state = 1; //hovering
}
}

state = 0; //doing nothing
}



Should I be using pointers for that or is it just a waste of time? Also, is my syntax correct?


Zahlman : Thanks for the tips, I just add C before my classes due to force of habit because this was the way the tutorials did it. Also, allegro does have GUI functions for buttons but they're ugly as hell so i made my own.
Quote:Original post by Le_Danse_Macabre
e.e; sorry everybody, just a few more questions.


* = the value of the variable the pointer is pointing to.

& = the address of the variable the pointer is pointing to.

That's right, right?

And a thing about my CheckState function. I'm not using the function to obtain the values of the mouse x/y or to return them, in fact i'm just using it to check them. like so:

void CButton::CheckState(volatile int *mx, volatile int *my, bool clicking)
{
if((*mx > x) && (*mx < x+50))
{
if((*my > y) && (*my < y+15))
{
if(clicking)
state = 2; // clicked
if(!clicking)
state = 1; //hovering
}
}

state = 0; //doing nothing
}



Should I be using pointers for that or is it just a waste of time? Also, is my syntax correct?


Zahlman : Thanks for the tips, I just add C before my classes due to force of habit because this was the way the tutorials did it. Also, allegro does have GUI functions for buttons but they're ugly as hell so i made my own.


this is a prime example of when not to use pointers at all. the only thing youve done here is kill efficency... why the volatile modifier? it forces the compiler to reload the value from system memory every access, without any need.

also, as you are not modifing the values there is no point. just use

void CButton::CheckState(int mx, int my, bool clicking)

and also use an enum:
button.h
class Button{private:enum States{    NOTHING,    HOVERING, // or HAS_FOCUS    CLICKING,};State state;public:/*...*/};

its clearer to everyone...
Thanks.

I used the volatile modifier because allegro uses it when to store it's mouse x and y...i just did it because of that, i've actually never seen or heard of the volatile modifier...I should just type cast then as a normal integer? Also, could you explain what a volatile int does?
Quote:Original post by Le_Danse_Macabre
Thanks.

I used the volatile modifier because allegro uses it when to store it's mouse x and y...i just did it because of that, i've actually never seen or heard of the volatile modifier...I should just type cast then as a normal integer? Also, could you explain what a volatile int does?


volatile tells the compiler that you dont reeally own the variable. a volatile int is an int that could change at any time. the compiler assumes all other variables are owned by it and it can do whatever it wants with them, like stick them in a register for the duration of a function. because the compiler can no longer gaurentee that the value wont change without it knowing, it has to check the value before it uses it. every time. thats not as fast as it could be...
Quote:Original post by Zahlman
....
- 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.
...
But it does add useful information to me and anybody else who uses it, if you follow the common rule that a C prefix indicates a concrete class. Then it's easy to tell an interface from its implementation.


Pointers are needed as, someone said, with polymorphism. Classic example:

#include

// Interface class, dictates how concrete classes should be handled
// This class can't be instantiated on its own
class IAnimal
{
public:
virtual void Speak() = 0;
};

// Concrete specification of an IAnimal type
class CDog:
public IAnimal
{
public:
void Speak() { std::cout Speak();
animal2->Speak();

return 0;
}
Quote:Original post by Anonymous Poster
Pointers are needed as, someone said, with polymorphism. Classic example:


references can be used to gain polymorphic behaviour( and its source, not code for future reference )


class Shape{public:virtual void draw() = 0;};class Circle : public Shape{public:virtual void draw(){ cout << "Circle!\n"; }};class Square : public Shape{public:virtual void draw(){ cout << "Square!\n"; }};void drawShape( Shape& theShape ){   theShape.draw();}int main(){   Circle c;   Square s;   drawShape(c);   drawShape(s);   return 0;}
i still have to see a use for pointers where a reference cant do the same thing, and just because we had a somehow similar discussion about pointers/references on irc a while ago i will post a little code snipped, where most of 'us' would use pointers (dynamically allocating memory)...

char& stuff= *new char[12];memcpy(&stuff, "Hello World\0", 12);std::cout << &stuff << std::endl;delete[] &stuff

so pointers must die...


T2k

This topic is closed to new replies.

Advertisement