Creating string from variable name

This is a discussion on Creating string from variable name within the C++ Programming forums, part of the General Programming Boards category; Hello, Is it possible to create a string from a variable name? for example, say i have a variable type ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    26

    Creating string from variable name

    Hello,

    Is it possible to create a string from a variable name?

    for example, say i have a variable
    type myVariable;
    could I get a string as
    char* name = "myVariable"

    Hope this makes sense

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    yes if it's a custom class and you can just implement a function that returns a string with it's name in it.

    Code:
    string name = myVariable.name

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    26
    Thanks for the reply.

    Is that the only way, what if its not in a class, but if its just a simple primitive type variable?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by indigo0086 View Post
    yes if it's a custom class and you can just implement a function that returns a string with it's name in it.

    Code:
    string name = myVariable.name
    But then you'd have to manually set the name to whatever you called the variable, so what's the point?

    I believe you can use the # pre-processor command to write a macro that will print the variable name you pass to it: http://www.cppreference.com/preprocessor/sharp.html

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    26
    thanks cpjust, thats exactly what i'm looking for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  4. Creating String Functions
    By stickman in forum C++ Programming
    Replies: 8
    Last Post: 04-30-2006, 06:26 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21