473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

getting the name of an object

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
Jul 19 '05 #1
10 2799

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

Jul 19 '05 #2
Isn't there another way, without giving the name to the constructor as an
argument?
Jul 19 '05 #3

"Thomas Baier" <th****@tho-bai.de> wrote in message news:3f***********************@newsread4.arcor-online.net...
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.
Jul 19 '05 #4

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

Jul 19 '05 #5
On Mon, 08 Sep 2003 21:03:16 +0200, Thomas Baier <th****@tho-bai.de>
wrote:
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
Jul 19 '05 #6
Thomas Baier wrote:
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

Jul 19 '05 #7

Thomas Matthews <Th**********************@sbcglobal.net> wrote in message
news:nY***************@newssvr32.news.prodigy.com. ..
Thomas Baier wrote:
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

Jul 19 '05 #8
Mike Wahler wrote:

Thomas Matthews <Th**********************@sbcglobal.net> wrote in message
news:nY***************@newssvr32.news.prodigy.com. ..
Thomas Baier wrote:
> 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.
Jul 19 '05 #9
Thomas Matthews wrote:
Thomas Baier wrote:
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

Jul 19 '05 #10
Demanet Michael wrote:

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.
Jul 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: manish | last post by:
I have created a function, it gives more readability compared to the print_r function. As of print_r, it works both for array or single variable. I just want to add in it, the opton to view the...
5
by: David Bear | last post by:
Let's say I have a list called, alist. If I pass alist to a function, how can I get the name of it? alist = range(10) def afunction(list): listName = list.__name__ (fails for a list object)
2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
15
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
12
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc..,...
10
by: Peter Afonin | last post by:
Hello, I have a simple client-side form that is checking the domain availability on the domain registrar's server: <FORM action="https://www.webnames.ru/scripts/RegTimeSRS.pl" method="post">...
20
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
4
by: shashank kadge | last post by:
hi all, i am trying to get local admin users and groups on a windows server. here is the C# code that i am using...
0
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.