-
March 31st, 2006, 12:47 PM
#1
How do I access an instance's name?
I want to get the name of a classe's instance at runtime. Is this possible?
ie.
class MyClass
{
....
public:
void getInstanceName(); }
void getInstanceName()
{
cout << "Hi, I am " << ?????????? << endl; }
int main()
{
MyClass John; john.getInstanceName(); }
-
March 31st, 2006, 01:37 PM
#2
Re: How do I access an instance's name?
Originally Posted by TedG
I want to get the name of a classe's instance at runtime. Is this possible?
No. This is not built into the language.
You have to provide your own means and methods of doing this. For example, all your classes contain a string member that contains the name of the class.
Regards,
Paul McKenzie
-
March 31st, 2006, 02:34 PM
#3
Re: How do I access an instance's name?
There is another way, using RTTI, but it will give you the name as an implementation defined (which means would be different for different compiler implementations) C-style const character string. The code below shows how both can be done and note in the output of typeinfo::name(). Using a static member is the way to go if you really want to do it ... but it is not really needed. Moreover, you may want to have a virtual function do this job with a string member.
Code:
#include<iostream>
#include<typeinfo>
#include<string>
using namespace std;
class MyClass{
static string ClassName;
public:
virtual string getClassNameUsingMemberVar() const{
return ClassName;
}
virtual string getClassNameUsingRTTI() const{
return typeid(*this).name();
}
virtual ~MyClass(){}
};
class DerivedMyClass : public MyClass{
static string ClassName;
public:
virtual string getClassNameUsingMemberVar() const{
return ClassName;
}
virtual string getClassNameUsingRTTI() const{
return typeid(*this).name();
}
};
string MyClass::ClassName = "MyClass";
string DerivedMyClass::ClassName = "DerivedMyClass";
int main(){
DerivedMyClass derivedMyClassObject;
MyClass myClassObject;
MyClass* ptr = &derivedMyClassObject;
cout << ptr->getClassNameUsingMemberVar() << endl;
cout << ptr->getClassNameUsingRTTI() << endl;
ptr = &myClassObject;
cout << ptr->getClassNameUsingMemberVar() << endl;
cout << ptr->getClassNameUsingRTTI() << endl;
return 0;
}
The output that I got on VC++ 6.0 was:
Code:
DerivedMyClass
class DerivedMyClass
MyClass
class MyClass
I suggest not using the RTTI option for such a trivial thing and it is not also dependable because this is what the standard says about it:
Originally Posted by C++ Standards
const char* name() const;
Returns: an implementation-defined value.
Notes: The message may be a null-terminated multi-byte string, suitable for conversion and display as a wstring
So, it could even be a multi-byte string so char[] is not correctly usable with it.. you would need to use wstring to store the value and make any manipulations to get the name part that you want. But just so you know, there is no guarantee that the return of name() is not empty. Use the member variable solution. Making it static is a good idea. Hope this helps. Regards.
Last edited by exterminator; March 31st, 2006 at 02:41 PM.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
March 31st, 2006, 04:53 PM
#4
Re: How do I access an instance's name?
"Programs must be written for people to read, and only incidentally for machines to execute."
-
April 1st, 2006, 12:48 AM
#5
Re: How do I access an instance's name?
@exterminator: cheers, though I need the instance's name, at runtime ,without having to declare a string variable.
@RoboTact: errr... to associate output from that instance with the instance it self. (and would rather not hand feed the name as a string)
Thank you all though!
-
April 1st, 2006, 03:24 AM
#6
Re: How do I access an instance's name?
Originally Posted by TedG
@RoboTact: errr... to associate output from that instance with the instance it self. (and would rather not hand feed the name as a string)
Could you be more specific, please?
-
April 1st, 2006, 04:15 AM
#7
Re: How do I access an instance's name?
Originally Posted by SuperKoko
Could you be more specific, please?
Apologies...
Imagine
class People {......}
and
People Mike, John, Mary;
Mike John and Mary do different stuff:
John.sayHi();
how can you tell who said "Hi" at runtime?
more specifically Mike, John etc. generate many files. I would like to be able to mark those files as
"Instance_Name###.dat"
, where ### is a serial number, ie
"John000.dat", "John001.dat" etc.
without having to do something like
People John("John");
which looks really ugly to me.
Any workarounds welcomed!
-
April 1st, 2006, 04:26 AM
#8
Re: How do I access an instance's name?
Originally Posted by TedG
I want to get the name of a classe's instance at runtime. Is this possible?
Haaa, I understand now.
You don't want the name of the class, but the name of the variable containing the instance of the class!
Originally Posted by TedG
People John("John");
which looks really ugly to me.
No, it isn't.
You can write a macro, if you like...
In all cases, you must understand that an instance may be created without variable, so there are no instance name in that case.
In a std::vector, std::list, with new, new[], etc.
Also, variable usually have generic names, because they may contain a different person, depending on the procedure call...
IMHO, it does not make sense.
Things like:
Never appear in production code.
It only appears in test code/debugging code or didactic code.
In reality, variable names look more like:
Code:
void PayTax(People& TaxablePeople);
In any case, you must differenciate variable name and instances... It is a fundamental language concept, shared by all programming languages...
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
April 1st, 2006, 06:23 AM
#9
Re: How do I access an instance's name?
Moreover, an object of People named as John does not make anything different. What matters are the set of attributes that we have for a given class. I dont like naming such a class as People but rather Person. So, if I have a Person class - there would be attributes in the person class that would distinguish the different objects and add to their individual significance. The person class with have an ID, first name, last name, address, educational qualification - and many other attributes - and when you would want to get the name of the instance - that would actually mean the (first + last) name of the object and not the name of the variable.. that would not make sense. Variable names are just for our convenience. Once the code changes to the machine code - it is not the compilers obligation to keep the same names of the variables. What you are asking for does not make sense and the solution is having a name variable (non-static) in the class to spit out the information that you want. Hope this helps. Regards.
Last edited by exterminator; April 1st, 2006 at 06:26 AM.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
April 1st, 2006, 03:46 PM
#10
Re: How do I access an instance's name?
-
April 1st, 2006, 04:01 PM
#11
Re: How do I access an instance's name?
Originally Posted by 80Degrees
Spy++ ?
What do you mean.
Is not Spy++ an utility to get information about currently open windows?
It is not related to C++.
I hope that exterminator and me convinced you that your request does not make sense, and that such thing would be harmful.
A macro could be used to declare an variable initialized to an object having the same name than the variable. But it would just be evil.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
April 1st, 2006, 04:09 PM
#12
Re: How do I access an instance's name?
Originally Posted by SuperKoko
I hope that exterminator and me convinced you that your request does not make sense, and that such thing would be harmful.
A macro could be used to declare an variable initialized to an object having the same name than the variable. But it would just be evil.
Who cares whom to convince.
Justified usage of such thing is enum pattern, when you use objects as enum elements. And sure you can use macroses to avoid duplicating name of enum element as its intrinsic name.
"Programs must be written for people to read, and only incidentally for machines to execute."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|