Archived

This topic is now archived and is closed to further replies.

kaiel090x

pointers and references

Recommended Posts

As a game programmer, will there be any reasons I need to know pointers and references. They seem pointless to me, and the books Im reading give absolutely no reason to know either besides "to manipulate memory directly." yeah obviosly, but the hell would I need to do that, and in games, why would i need it. Please reply someone. Hey, I am new to C++, but I have worked with Flash, and I consider myself one of the best Flash Game programmers around. Truthfully, If flash could support more code(it has a limit, i think its like 100,000 lines) I would never use C++. I could make 2d games in flash, simply, put in plain words, I can do more than flash is capable of. I have never seena pointer or reference, and they are quite confusing, mainly because Im reading a book by Jesse Liberty on how to do it, and besides that, Im used to all variables being public. I see no reason I should need private variables.(as in classes)I can just use structs, they are easier, always public, and dont have a constructor and deconstructor, another thing Jesse Liberty cant write about. So, I understand I could use pointers for certain things, but why should I go through all the trouble of using a pointer to define a private variable when I can just make it public. I find no logic in it. ~ from the depths of the ocean Edited by - kaiel090x on August 12, 2001 5:23:24 PM

Share this post


Link to post
Share on other sites
Ok I''ll take it you haven''t programmed a lot before (or at least not worked with something like C++). So let me put it this way:

I CAN NOT IMAGINE HOW IT IS POSSIBLE TO PROGRAM WITHOUT POINTERS.

this is mainly because I have never really programmed with anything else before (well a bit of GWBasic and QBasic).

There are just so many examples of how pointers are more than useful. Here are a few examples

- with pointers you can dynamically allocate memory. ie you can make arrays which do not have a pre-specified size.

eg Bullet *MyBullets = new Bullet[size]

size can be a variable. if you tried to do something like Bullet MyBullet[size], you would get an error

- You can pass pointers to functions as parameters which means they can be changed inside the function.

eg void SetMyValues (int *a, int *b)
{
*a = 2;
*b = 3;
}

see here that you can actually set the value of ''a'' and ''b'' where as if they were not pointers, once the function returns, these two would be wiped from memory

- Thirdly there is just this whole idea that when you create a pointer inside a function, it is not wiped from memory when the function returns. This could allow you to do a whole bunch of things. eg you can make linked lists and have functions that add or remove new objects to the list, which if it wasn''t for pointers you could not do.

5 years ago I would say exactly what you said. what is the point of pointers? But when you actually start to use them you realize what powerfull tools they are and after a while you can''t imagine programming without them. My suggestion is try to use pointers although at first you might not realize why exactly you are using them.

Anyway those were my thoughts. Of course I could be completely wrong. Hope it helped anyway.

Thanks

Share this post


Link to post
Share on other sites
Being as the other person didnt tell u what refrences are used for il ggive u a breaf explination. refrences are used for passing information to a function in much the same way u would pass a value normally eg

  

void function(int x)
{
//code

}

void main()
{
int x;

function(x);
}

//now if u do this


void function(int & x) //use the & operator there u c?!

{
//code

}

void main()
{
int x;

function(x);
}
[/souce]

Then the variable x is shared between main() and function(), but in the first excample a new varialbe x is created and the data is copied from one variable to another which can take a ling time


~prevail by daring to fail~

Share this post


Link to post
Share on other sites
Since the other 2 guys covered the tech details I''ll just comment about public/private variables. Being a programmer isn''t about writing code, any monkey can do that, being a good programmer is about designing code that you (& others) can work on easily. Hence OO. You should really read an OO design book as well as a C++ programming book, then you will not only know how to use the features of C++ (public/private/classes/etc) but you will know when to use the features & why you should use them.
Otherwise you will hang yourself by your own sphagatti code....which is bad (believe me, I''ve done it!)

Brad

Share this post


Link to post
Share on other sites
kaiel090x, please don''t post the same question across numerous forums, and please don''t post questions that have already been posted in the last 2 or 3 days!

You have plenty of replies on the pointer issue in your other thread here, and the issues regarding public/private data have been amply raised here, here and here.

Use the search facility to look for information relevant to you. We don''t mind helping people here, but that doesn''t mean we''re happy spelling out the same basic points every 2 days just because someone couldn''t be bothered to spend a few minutes browsing the forums and using the search facility.

Share this post


Link to post
Share on other sites