» Keyword: this (pointer) by Valmont |
|
(Login to remove green text ads)
The Problem
The Problem
Straight into code:
Code:
//main.cpp
#include <iostream>
#include <string>
using namespace std; //Ok for this very small project.
class CSomeClass
{
public:
void Print(string text)
{
cout<<text<<endl<<endl;
}
};
int main(int argc, char* argv[])
{
CSomeClass obj_1;
obj_1.Print ("Object 1 made me print this");
return 0;
}
Just about the most simple working code with a class and some feedback.
The code is not getting more complex than this, but we need to add just one more instance of class CSomeClass:
Code:
#include <iostream>
#include <string>
using namespace std; //Ok for this small project.
class CSomeClass
{
public:
void Print(string text)
{
cout<<text<<endl<<endl;
}
};
int main()
{
CSomeClass obj_1;
CSomeClass obj_2;
obj_1.Print ("Object 1 made me print this");
obj_2.Print ("Object 2 made me print this");
return 0;
}
Ever wondered how CSomeClass::Print() knew which text to print correctly?
One could say: 'Well, you've typed it. It's right there on paper.'
But that wasn't the question.
How did void CSomeClass::Print() knew?
Knowing the answer to my question is the whole point of this tutorial.
Remember, there is only one unique void CSomeClass::Print(char* text). Just look at the class above.
See the image below for a demonstration of this problem:

Before an answer is given, a small recap on objects will be given first.
|
|