getting the name of an object

T

Thomas Baier

Hi there,

I've written a simple tree class and want to make a function that shows a
full tree with all its nodes.
My problem is that I do not know how to get the name of an object. I'd like
to have something like
class Tree
{

char *name;

Tree() {
name = this->variable_that_contains_name_of_object;
}
}
Tree one;
cout << one.name;

that results in the output:
one

Thanks for help


Thomas
 
M

Mike Wahler

Thomas Baier said:
Hi there,

I've written a simple tree class and want to make a function that shows a
full tree with all its nodes.
My problem is that I do not know how to get the name of an object. I'd like
to have something like
class Tree
{

char *name;

Tree() {
name = this->variable_that_contains_name_of_object;
}
}
Tree one;
cout << one.name;

that results in the output:
one

This cannot be done 'directly', since once the code is
compiled, there are no more 'names' of objects. Everything
is an address or an offset.

You could, however, create a data member which you store
your 'name' in as a string.

#include <string>

class X
{
std::string m_name;
public:
X(const std::string& n) : m_name(n) {}
const std::string& name() const { return m_name; }
};

int main()
{
X obj("one"); /* create object with 'name' of "one" */

std::string s(obj.name()); /* retrieve object's name and store
in another string */

return 0;
}

-Mike
 
T

Thomas Baier

Isn't there another way, without giving the name to the constructor as an
argument?
 
R

Ron Natalie

Thomas Baier said:
Isn't there another way, without giving the name to the constructor as an
argument?

No. The language provides no way to get the identifier names. In general, they
do not exist at runtime.
 
M

Mike Wahler

Thomas Baier said:
Isn't there another way, without giving the name to the constructor as an
argument?

No there is not. I already told you that.
Unless you're perhaps using an interpreter instead
of a compiler, there *are* no identifiers in the
executable, so of course they cannot be 'looked up'.
An interpreter might have means to do this, but that's
outside the purview of the language.

-Mike
 
T

tom_usenet

Hi there,

I've written a simple tree class and want to make a function that shows a
full tree with all its nodes.
My problem is that I do not know how to get the name of an object. I'd like
to have something like
class Tree
{

char *name;

Tree() {
name = this->variable_that_contains_name_of_object;
}
}
Tree one;
cout << one.name;

that results in the output:
one

How is this useful? You're passing something about the source code
organization into the output of the executable. This sounds like it
might be related to debugging, in which case the solution is to use a
debugger.

It would be helpful if you described exactly what you want to use this
facility for, and why you need it.

Tom
 
T

Thomas Matthews

Thomas said:
Isn't there another way, without giving the name to the constructor as an
argument?

Yes, supply the name as a static constant:

class MyClass
{
public:
const char * get_class_name(void)
{ return class_name;} // option #2
static const char class_name[];
};

const char MyClass::class_name[] = "MyClass";


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
M

Mike Wahler

Thomas Matthews said:
Thomas said:
Isn't there another way, without giving the name to the constructor as an
argument?

Yes, supply the name as a static constant:

class MyClass
{
public:
const char * get_class_name(void)
{ return class_name;} // option #2
static const char class_name[];
};

const char MyClass::class_name[] = "MyClass";

I think he's looking for the identifier of each
specific instance, not for the name of the class.

-Mike
 
T

Thomas Baier

Mike said:
Thomas Matthews said:
Thomas said:
Isn't there another way, without giving the name to the constructor as an
argument?

Yes, supply the name as a static constant:

class MyClass
{
public:
const char * get_class_name(void)
{ return class_name;} // option #2
static const char class_name[];
};

const char MyClass::class_name[] = "MyClass";

I think he's looking for the identifier of each
specific instance, not for the name of the class.

-Mike
Right. I want to have a Tree class were you can get the name of every single
tree in order to show the hole tree graphically.
 
D

Demanet Michael

Thomas said:
Thomas said:
Isn't there another way, without giving the name to the constructor as an
argument?


Yes, supply the name as a static constant:

class MyClass
{
public:
const char * get_class_name(void)
{ return class_name;} // option #2
static const char class_name[];
};

const char MyClass::class_name[] = "MyClass";
In that cas, isn't it better using RTTI mechanism instead of your method ?

Michael
 
R

Ron Natalie

Demanet said:
In that cas, isn't it better using RTTI mechanism instead of your method ?

Not if you want the names to be meaningful. The typeinfo::name() field isn't
guaranteed to be overly useful.
 

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
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top