Jump to content
  • Advertisement
Sign in to follow this  
Raxis

pointers = stress

This topic is 3764 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

So I've started on pointers and actually finished the documentation of it. I can figure out what is the use of them? I know it says "It's a very useful and powerful tool". I don't get how this is. All it is doing is pointing to something or what's inside. What exactly makes them powerful/useful? -Thank you!

Share this post


Link to post
Share on other sites
Advertisement
To use a text-book example:

Lets say you've got 2 classes: Student and Subject. Each student can be enrolled in several subjects.
class Student
{
public:
std::string name;
};

class Subject
{
public:
std::string code;
};

Student John, Frank;
John.name = "John";
Frank.name = "Frank";

Subject subjects[3];
subjects[0].code= "ITC101";
subjects[1].code= "ITC201";
subjects[2].code= "ITC343";

Now, Lets say John is doing subject #101 and #201, and Frank is doing #201 and #343. How do we represent this?

We *could* do it without pointers, by duplicating a key variable from the subject within the student, like this:
class Student
{
public:
std::string name;
std::vector<std::string> subjectCodes;
};
...
John.subjectCodes.push_back( subjects[0].code );
John.subjectCodes.push_back( subjects[1].code );
//but then when we want to do something with one of John's subjects, we have to perform a search:
for( int i=0; i<3; ++i )
{
if( subjects.code == John.subjectCodes[0] )
subjects.DoSomething();
}


A better way to link objects together is to just use pointers:
class Student
{
public:
std::string name;
std::vector<Subject*> subjects;
};
...
John.subjects.push_back( &subjects[0] );
John.subjects.push_back( &subjects[1] );
//now when we want to do something with one of John's subjects, we can use the pointer to directly access it:
John.subjects[0]->DoSomething();

Share this post


Link to post
Share on other sites
I agree when you are first learning pointers are too much of a hassle to deal with so do yourself a favor and stick with references as long as you can.
You shouldn't run into pointers anyways if you are using a good C++ book to learn from. I don't think "Accelerated C++" even uses them until near the end.
In you deal with C code or game programming you'll see them alot more often since C doesn't have references.

"
Why is "this" not a reference?
Because "this" was introduced into C++ (really into C with Classes) before references were added. Also, I chose "this" to follow Simula usage, rather than the (later) Smalltalk use of "self".
"

Personally, I have to use them right now in my Allegro game programming since alot of drawing commands return an address which you need to store in a pointer.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!