Thomas Baier <th****@tho-bai.de> wrote in message
news:3f**********************@newsread2.arcor-online.net...
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