M
MN
Hi All,
Can any one show me how to get the instance's name of a given class in
the next code ?
#include <iostream>
using namespace std;
class test
{
int count;
public:
test ();
~test();
int total(int num, test &instance_of_test);
};
test::test(){
count = 0;
};
test::~test(){};
int test::total(int num, test &instance_of_test)
{
count += num;
cout << "Added number " << num << " from instance \"" <<
&instance_of_test << "\" of class test\n";
return count;
}
int main (){
// Declare 2 instances of class "test"
test tst;
test tst1;
int x = 0;
int y = 0;
for (int i = 0; i < 3; ++i)
{
x = tst.total(i, tst);
}
cout << "\nTotal is "<< x << "\n";
cout << "\nUse another instance of class \"test\":\n" << endl;
for (int j = 0; j < 2; ++j)
{
y = tst.total(j, tst1);
}
cout << "\nTotal is "<< y << "\n";
return 0;
}
After execution I have these next lines:
Added number 0 from instance "0x7fff3c1ce700" of class test
Added number 1 from instance "0x7fff3c1ce700" of class test
Added number 2 from instance "0x7fff3c1ce700" of class test
Total is 3
Use another instance of class "test":
Added number 0 from instance "0x7fff3c1ce6f0" of class test
Added number 1 from instance "0x7fff3c1ce6f0" of class test
Total is 4
So what to do to change "0x7fff3c1ce700" to "tst" and "0x7fff3c1ce6f0"
to "tst1" ?
Can any one show me how to get the instance's name of a given class in
the next code ?
#include <iostream>
using namespace std;
class test
{
int count;
public:
test ();
~test();
int total(int num, test &instance_of_test);
};
test::test(){
count = 0;
};
test::~test(){};
int test::total(int num, test &instance_of_test)
{
count += num;
cout << "Added number " << num << " from instance \"" <<
&instance_of_test << "\" of class test\n";
return count;
}
int main (){
// Declare 2 instances of class "test"
test tst;
test tst1;
int x = 0;
int y = 0;
for (int i = 0; i < 3; ++i)
{
x = tst.total(i, tst);
}
cout << "\nTotal is "<< x << "\n";
cout << "\nUse another instance of class \"test\":\n" << endl;
for (int j = 0; j < 2; ++j)
{
y = tst.total(j, tst1);
}
cout << "\nTotal is "<< y << "\n";
return 0;
}
After execution I have these next lines:
Added number 0 from instance "0x7fff3c1ce700" of class test
Added number 1 from instance "0x7fff3c1ce700" of class test
Added number 2 from instance "0x7fff3c1ce700" of class test
Total is 3
Use another instance of class "test":
Added number 0 from instance "0x7fff3c1ce6f0" of class test
Added number 1 from instance "0x7fff3c1ce6f0" of class test
Total is 4
So what to do to change "0x7fff3c1ce700" to "tst" and "0x7fff3c1ce6f0"
to "tst1" ?