Thread: Object prints out its variable name

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Houston, TX
    Posts
    5

    Question Object prints out its variable name

    Hi!

    I wanted to do something like

    Code:
    MyClass var1, var2;
    
    cout << var1.name() << var2.name() << endl;
    This should printout

    var1 var2

    Note: I am not explicitly passing the object what variable name its assigned. Is there any way the object can figure out this information on its own..?

    Thanks!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by garf13ld View Post
    Note: I am not explicitly passing the object what variable name its assigned. Is there any way the object can figure out this information on its own..?
    It's impossible.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Location
    Houston, TX
    Posts
    5

    Unhappy

    I mean, I know as far as the language is concerned its impossible.. But could there be any ugly compiler hack that may allow this..?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could, however, write a macro that generates a string from it's input value:
    Code:
    #define mkstring(x) #x
    Using this, we can do something like this:
    Code:
    class MyObject
    {
    private:
       char *iName
     ....
    public:
       MyObject(char *name) : iname(name) { ... };
       ...
    }
    
    #define INSTANCIATE(type, name) type name(#name)
    
    int main()
    {
        INSTANCIATE(MyObject, var1);
        INSTANCIATE(MyObject, var2);
    
        cout << var1.name << var2.name << endl;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Normally, variable names don't even exist in the compiled EXE file. The machine code just keeps track of the variable's address in memory, it doesn't need a name. (If you use a de-compiler, it will make-up it's own variable names.)

    I've never tried using <typeinfo>, but it might work for you. I think it can give you an object name, but I have no idea if it also keeps track of variable names.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why would you need to print out the variable name?

    You could add a string parameter in the constructor that takes the name... but again, what's the point?

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by DougDbug View Post
    I've never tried using <typeinfo>, but it might work for you. I think it can give you an object name, but I have no idea if it also keeps track of variable names.
    typeinfo only gives information about types, not about particular variables.

  8. #8
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    You could add a string or char* which holds the name in your class, but there is no default way to get a variable name.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    matsp's solution is the only feasible way to do it with the language, but as he would agree it is an ugly, ugly, unnecessary hack.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    Registered User
    Join Date
    Sep 2007
    Location
    Houston, TX
    Posts
    5
    Well the reason I wanted to do something like this is because I am designing a matrix class and when I do something like

    Code:
    Matrix jacobian;
    ...
    cout << jacobian;
    I want it to print out

    jacobian = [.....]

    This is similar to what Matlab does. Obviously I could include a string within the class and make the developer explain what the matrix represents, but I figured if something like this was indeed possible it would encourage the developer to use descriptive variable names.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >This is similar to what Matlab does.
    Matlab is much higher level than C++. If you want a scripting language, use one or write an interpreter for some pseudo-C++. If you want C++, you have to realize that compile-time symbols are generally going to be lost during compilation. The only way to retain them is to store them separately at runtime with your own machinery.
    My best code is written with the delete key.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, explaining your purpose and how you want to use it will also help us give you the best help.

    In this particular case, you can solve the problem in a manner like what I explained above (or, of corurse just adding it's name to the creation of it).

    However, there are multiple problems with "naming" a variable. Consider:
    Code:
    int func(type &var1)
    {
     ... 
    }
    
    int func2(type &var2)
    {
       ... 
        func1(var2);
        ... 
    }
    
    int main()
    {
       type someVariable;
       type **ptr;
       int x;
       
       x = func1(someVariable);
       ptr = new type[10];
       ... 
       func2(*ptr[5]); 
    }
    Now, what is the "name" of var1 inside func1:
    - when it's called with someVariable?
    - when it's been called from func2 with a pointer indexed?


    Having "meaningfull" variable names is about style, and a goor programmer/sw engineer will use good style - what the definition of good style is of course also slightly depending on several factors, and what one person thinks is a good name isn't necessarily what everyone else thinks is a good name.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    #define PRINT_NAME_AND_VAR( var )   cout << #var << " = " << var;
    ...
    Matrix jacobian;
    ...
    PRINT_NAME_AND_VAR( jacobian );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  2. error display variable value/type
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 07-08-2008, 04:24 PM
  3. A pointer question.
    By joenching in forum C++ Programming
    Replies: 7
    Last Post: 03-20-2008, 04:10 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM