Results 1 to 5 of 5

Thread: Is there any way to get the string-name of the member of a struct or class ?

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Is there any way to get the string-name of the member of a struct or class ?

    If I have a struct (in example) Config { double n_users; int n_n;};

    Is there any way to write some code which gives me the name of the member of a struct ? I'd want to use it to write its name into an ascii file .
    In example (pseudocode)
    config.n_users=4; save_data(config_n.users.to_string)
    function save_data (string key)
    ???
    Thanks

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is there any way to get the string-name of the member of a struct or class ?

    You can use a #define with the stringizing operator:

    eg:
    Qt Code:
    1. #define SAVE_DATA(token) save_data(#token, token)
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Is there any way to get the string-name of the member of a struct or class ?

    Yes you can do this using Qt Meta Object System, it is not very straight forward you need to refactor you exsiting code, to a good extent to make it work.

    http://doc.qt.nokia.com/4.7-snapshot/object.html

  4. #4
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is there any way to get the string-name of the member of a struct or class ?

    Thanks squidge, but in your proposal I'd have to write two macros one to store and another one to retrieve the data.
    An if my struct is (in example) my_cofig.my_value .... can you show me how can I write the both macros ?
    (the examples I have seen on the web are simple.)
    My struct are going to have more than 100 elements....

    Santosh. Thanks also. But How can I specify types (doubles, ints, strings, etc ) ?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is there any way to get the string-name of the member of a struct or class ?

    If you teach your structure to save itself from within its own scope (i.e. from its method), you will not need to reference the structure name anywhere and you can use the solution squidge offered you (if that's not ok then simply write a macro accepting two arguments -- the structure variable name and the member in the structure). If you don't want to write two lines of code (although I have no idea what two lines of code you mean) then write another macro that will execute the two relevant statements and you'll do it with one line of code.

    Qt Code:
    1. #define SAVE_DATA(var, token) save_data(#token, var.token)
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Manipulating member name to be controlled by string
    By jwflammer in forum Qt Programming
    Replies: 3
    Last Post: 28th February 2011, 02:19
  2. Struct into a C++ class
    By franco.amato in forum General Programming
    Replies: 24
    Last Post: 30th September 2010, 16:13
  3. accessing static member variables of one class in another class
    By jasonknight in forum General Programming
    Replies: 5
    Last Post: 6th September 2010, 14:53
  4. Replies: 4
    Last Post: 29th May 2010, 12:56
  5. QDataStream class/struct & stream operators
    By darksaga in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2008, 19:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.