Results 1 to 5 of 5

Thread: Help with possible array with function names

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Help with possible array with function names

    Is there an easier way to write this code by using like an array to store the names of functions?
    for example something like this
    Qt Code:
    1. QString functions [] = { "food()", "calorie" };
    2. for(int i=0;i<2;i++){
    3. output << entrynum->functions[i]
    4. }
    To copy to clipboard, switch view to plain text mode 
    I tried doing something like this but doesn't work for me because of the ->

    The code I have which works as I wish but doesn't look very neat unfortunately. =/
    Qt Code:
    1. for(int i = 0; i < 9; i++){
    2. string filename = "/";
    3. string number;
    4. stringstream convert;
    5. convert << i;
    6. number = convert.str();
    7. filename.append(number);
    8. filename.append(".txt");
    9. string entrynum = "entry";
    10. entrynum.append(number);
    11.  
    12. //cout << "The filename is: " << filename << endl;
    13. QString filename2 = QString::fromStdString(filename);
    14. QFile i(QDir::homePath() + filename2);
    15. if (i.open(QIODevice::WriteOnly)) {
    16. QTextStream output(&i);
    17. output.setCodec("UTF-8");
    18.  
    19. for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
    20. ABItem *entrynum =
    21. static_cast<ABItem *>(treeWidget->topLevelItem(i));
    22. if(filename == "/0.txt"){
    23. output << entrynum->food() << "\r\n" << endl;
    24. output << endl;
    25. } else if(filename == "/1.txt"){
    26. output << entrynum->calorie() << "\r\n" << endl;
    27. output << endl;
    28. } else if(filename == "/2.txt"){
    29. output << entrynum->protein() << "\r\n" << endl;
    30. output << endl;
    31. } else if(filename == "/3.txt"){
    32. output << entrynum->carb() << "\r\n" << endl;
    33. output << endl;
    34. } else if(filename == "/4.txt"){
    35. output << entrynum->carb() << "\r\n" << endl;
    36. output << endl;
    37. } else if(filename == "/5.txt"){
    38. output << entrynum->fat() << "\r\n" << endl;
    39. output << endl;
    40. } else if(filename == "/6.txt"){
    41. output << entrynum->sugar() << "\r\n" << endl;
    42. output << endl;
    43. } else if(filename == "/7.txt"){
    44. output << entrynum->sodium() << "\r\n" << endl;
    45. output << endl;
    46. } else if(filename == "/8.txt"){
    47. output << entrynum->fiber() << "\r\n" << endl;
    48. output << endl;
    49. }
    50. }
    51. }
    52. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help with possible array with function names

    QString functions [] = { "food()", "calorie" };
    for(int i=0;i<2;i++){
    output << entrynum->functions[i]
    }
    What are you trying to achieve? It looks like what you want is a function dispatching mechanism, where you call a different function based on some key.

    Use a std::map< QString, "pointer to member function" > or a QHash( QString, "pointer to member function" ), where the QString is the key (like "calorie", or "protein") and the value (i.e. the "pointer to member function" part) is a pointer to the function you want called for that keyword. If you don't know how to write the C++ code to declare a "pointer to member function", Google for that phrase. The 4th hit is a PDF that give a good review.

    To create the lookup (dispatch) table:

    std::map< QString, "pointer to member function" > functionMap;

    functionMap[ QString( "food" ) ] = "pointer to Food::food()";
    functionMap[ QString( "calorie" ) ] = "pointer to Food::calorie()";
    functionMap[ QString( "protein" ) ] = "pointer to Food:rotein()";

    etc.

    To use the dispatch table, you simply do this:

    Food * myFood = new Food( "BigMac" );
    QString key = "calorie";

    output << myFood->(*functionMap[ key ])();

    Of course, you can use anything for the key part - I've simply used the function names themselves for simplicity.

  3. #3
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: Help with possible array with function names

    Thx for reply but I'm a bit confused on the Mapping I might have to look up a few websites I tried a bunch of different things heres the last thing I tried:
    Qt Code:
    1. map<QString,QString>functions;
    2. functions["food"] = entrynum->food();
    3. functions[0]
    To copy to clipboard, switch view to plain text mode 
    Heres what im trying to do.
    say I have a QString called functions. I want it so I can do output << functions. Then in place of functions it would put "entrynum->food()". so basically a container or something to hold the names of my functions I will be calling so that way I can do something like functions[i] instead of entrynum->food, entrynum-> calories ect.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help with possible array with function names

    I am confused about what you are trying to do. Are you trying to output the names of the functions in your class, or are you trying to output the values returned by those functions after you look up the function by name?

    It would help if you would give an example of what you expect the output to look like (not the code to do it, the output text itself).

  5. #5
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: Help with possible array with function names

    I'm just trying to simplify
    Qt Code:
    1. for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
    2. ABItem *entrynum =
    3. static_cast<ABItem *>(treeWidget->topLevelItem(i));
    4. if(filename == "/0.txt"){
    5. output << entrynum->food() << "\r\n" << endl;
    6. output << endl;
    7. } else if(filename == "/1.txt"){
    8. output << entrynum->calorie() << "\r\n" << endl;
    9. output << endl;
    10. } else if(filename == "/2.txt"){
    11. output << entrynum->protein() << "\r\n" << endl;
    12. output << endl;
    13. } else if(filename == "/3.txt"){
    14. output << entrynum->carb() << "\r\n" << endl;
    15. output << endl;
    16. } else if(filename == "/4.txt"){
    17. output << entrynum->carb() << "\r\n" << endl;
    18. output << endl;
    19. } else if(filename == "/5.txt"){
    20. output << entrynum->fat() << "\r\n" << endl;
    21. output << endl;
    22. } else if(filename == "/6.txt"){
    23. output << entrynum->sugar() << "\r\n" << endl;
    24. output << endl;
    25. } else if(filename == "/7.txt"){
    26. output << entrynum->sodium() << "\r\n" << endl;
    27. output << endl;
    28. } else if(filename == "/8.txt"){
    29. output << entrynum->fiber() << "\r\n" << endl;
    30. output << endl;
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 
    to something more like this:
    Qt Code:
    1. for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
    2. ABItem *entrynum =
    3. static_cast<ABItem *>(treeWidget->topLevelItem(i));
    4. if(filename == filename){
    5. output << entrynum->array[i] << "\r\n" << endl;
    6. output << endl;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    The orig. code works fine its just bulky and gross looking was trying to make it look nicer.


    Added after 15 minutes:


    PS: The outputs are 9 Text files which are named 0.txt, 1.txt, 2.txt ect... and they contain seperate data inside of each for example 0.txt has:
    "Food1
    Food2" inside of the file
    Last edited by giblit; 22nd March 2013 at 23:00.

Similar Threads

  1. Pass struct array to function
    By willbeas_SMU in forum General Programming
    Replies: 2
    Last Post: 19th March 2012, 17:05
  2. Replies: 5
    Last Post: 23rd September 2011, 09:44
  3. Using array with QAxWidget::dynamicCall() function
    By tom0485 in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2010, 10:31
  4. QtScript : passing array as argument into function
    By derek_r in forum Qt Programming
    Replies: 4
    Last Post: 27th October 2007, 10:46
  5. Qt causes conflicting function names
    By thomaspu in forum Qt Programming
    Replies: 6
    Last Post: 14th September 2007, 15:30

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
  •  
Qt is a trademark of The Qt Company.