CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2006
    Posts
    3

    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();
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How do I access an instance's name?

    Quote 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

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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:
    Quote 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.

  4. #4
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: How do I access an instance's name?

    What do you need it for?
    "Programs must be written for people to read, and only incidentally for machines to execute."

  5. #5
    Join Date
    Mar 2006
    Posts
    3

    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!

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: How do I access an instance's name?

    Quote 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?

  7. #7
    Join Date
    Mar 2006
    Posts
    3

    Re: How do I access an instance's name?

    Quote 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!

  8. #8
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: How do I access an instance's name?

    Quote 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!

    Quote 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:
    Code:
    People John;
    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()!

  9. #9
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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.

  10. #10
    Join Date
    Feb 2006
    Posts
    162

    Re: How do I access an instance's name?

    Spy++ ?

  11. #11
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: How do I access an instance's name?

    Quote 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()!

  12. #12
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: How do I access an instance's name?

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured