Instance's name of a given class

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" ?
 
D

DerTopper

Hi All,

Can any one show me how to get the instance's name of a given class in
the next code ?

What you want to do is impossible with C++. I'd would go even further
and state that no recent compiler will do what you want.

Why do you want to have the names of the instances? If it is just for
debugging purposes, then you had better get hold of a decent compiler.

Regards,
Stuart
 
M

MN

I'm working with g++/gdb under fedora8.
I thought that there is a standard mechanism, which extracts class's
instance automatically.
So what can I do, is to add a string in function "test::total" like in
the next code:

#include <iostream>
using namespace std;


class test
{
int count;
public:
test ();
~test();
int total(int num, char *instance_of_test);
};

test::test(){
count = 0;
};

test::~test(){};

int test::total(int num, char *instance_of_test)
{
count += num;
cout << "Added number " << num << " from instance \"" <<
instance_of_test << "\" of class test\n";
return count;
}


int main (){
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;
}
As result I get :
Added number 0 from instance "tst" of class test
Added number 1 from instance "tst" of class test
Added number 2 from instance "tst" of class test

Total is 3

Use another instance of class "test":

Added number 0 from instance "tst1" of class test
Added number 1 from instance "tst1" of class test

Total is 4
 
H

Helge Kruse

MN said:
I'm working with g++/gdb under fedora8.
I thought that there is a standard mechanism, which extracts class's
instance automatically.

No, but gdb reads debug info and can say that's this variable. No way in
normal execution of the program.

Helge
 
E

Erik Wikström

Hi All,

Can any one show me how to get the instance's name of a given class in
the next code ?

#include <iostream>

#include said:
using namespace std;

class test
{
int count;

const std::string name;

test (const std::string& name);
~test();
int total(int num, test &instance_of_test);
};



test::test(const std::string& name)
: count(0), name(name)
{ }
test::~test(){};

int test::total(int num, test &instance_of_test)
{
count += num;

cout << "Added number " << num << " from instance \"" << name <<
"\" of class test\n";
return count;
}

int main (){
// Declare 2 instances of class "test"

test tst("tst");
test tst1("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;
}

I have neither tested, nor compiled the code, but I think you get the idea.
 
M

Marcelo Pinto

MN said:
class test
{
        int count;
public:
        test ();
        ~test();
        int total(int num, char *instance_of_test);

The instance_of_test should be of type const char * to be used in the
lines bellow
 
J

James Kanze

The instance is pointed to by the 'this' pointer. That's the
mechanism.
As far as the object is concerned, it has no name. It's your
imagination that there is "tst" or "tst1" or whatever. It
only exists while the program is in the source form. After
it's compiled, there is no single way to get back to the
source form. You can rewrite your program by changing the
names of your objects and classes to any of the allowed
combinations of symbols and get exactly the same final
compiled program. There are few limitations on that, like
'main' has to be 'main' and cannot be 'start', or that
'std::cout' has to be 'std::cout' in order to be picked up
from the library.

Just to expand on this: what would the name of the intance be if
I used a temporary "text()"? Or for an object allocated by new?
As Victor said, names are purely compile time. Some objects
(most, in a lot of real applications) don't have any name;
others may have more than one name (depending on what you
consider a "name").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top