pointers = stress
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!
To use a text-book example:
Lets say you've got 2 classes: Student and Subject. Each student can be enrolled in several subjects.
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:
A better way to link objects together is to just use pointers:
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();
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement