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

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    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.

    Code:
    class MyClass
    {
    ....
    public:
        void getInstanceName();
    }
    
    void getInstanceName()
    {
        cout << "Hi, I am " << ?????????? << endl;
    }
    
    int main()
    {
    MyClass John;
    john.getInstanceName();
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I want to get the name of a classe's instance at runtime. Is this possible?
    Sure. Define a method in the class that returns the class name.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you mean you want it to return "John" -- the answer is no.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Well, actually there is a way you can do that. Add a string object to the class to hold the instance name then add a constrol that takes the instance name as a parameter. But of course there is no guarentee that getInstanceName() will return the right name if you pass the wrong name to the ctor (garbage in, garbage out principal).

    Code:
    class MyClass
    {
    public:
       MyClass(const char* instanceName) {m_instance = instance_name;}
      std::string getInstanceName() {return m_instance;}
    private:
       std::string m_instance;
    }
    
    ...
    ...
    int main()
    {
       MyClass John("John");
    }

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    Quote Originally Posted by Ancient Dragon
    Well, actually there is a way you can do that. Add a string object ....
    Yes, though this is surely cheating, isn't it? The whole idea is to avoid hand feeding a string to the constructor. "John-John" just looks ugly to me! Thanks anyway.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why do you need that? Other than for some advanced topics I can't think of a reason.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The name of a variable is seen by the compiler, and is not accessible at run time.

    If you want an object to be aware of it's name, YOU need to implement functionality so it is, with an approach like that suggested by Ancient Dragon.

    Of course, this approach doesn't stop an object declared as
    Code:
       MyClass John("Bill");
    reporting that it's name is Bill.

    If you want to do away with the typing, use a macro;
    Code:
    #define CreateMyClass(x)  MyClass x(#x)
    
    int main()
    {
        CreateMyClass(John);
    }
    As long as the programmer always uses the CreateMyClass macro, an object named John will always report its name as John. The catch with this approach is that there is no way to force the programmer to use the macro. And the fact that a macro has other effects which are not always desirable, because it works by pasting of text before the compiler sees the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of access labels in class definition
    By Mario F. in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2006, 07:13 AM
  2. Classes and Destroyed Instances
    By FWGaming in forum C++ Programming
    Replies: 3
    Last Post: 07-12-2005, 10:02 PM
  3. Replies: 3
    Last Post: 09-22-2003, 09:48 PM
  4. Direct disk access in DOS
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-26-2002, 02:52 PM