Thread: template class // normal class

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You'd probably want to use a [constant] string to hold the default values, unless there are a lot of them.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are these settings known at compile time? In other words, do you know what settings your program needs when you write the program? I'm not talking about the values entered by the user, I'm talking about what those values mean. Can the user type in anything they want, or does it have to have meaning? If it has specific meaning, create a variable for each specific setting. That variable can be a class that holds the name, type, and value. That class can set the value to the default and then provide a function to change the value if the user enters something different. You can have a different class if you want for the different types of settings (string, int, or whatever), but you do that at compile time instead of using a variant that doesn't know what it is until run-time.

  3. #18
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I have a config file reader class that a friend of mine coded. Internally it stores everything as strings, in a std::map<std::string, std::string>, where the first string is the name of the setting, and the second is the value.

    The public interface of the class, however, has functions like GetInt(std::string name) which basically does string->int conversion. How you handle failure is up to you -- you can return 0, you could throw an exception, or you could set an error flag. You could even have a second parameter which specifies the default value, which is returned if the variable doesn't exist/isn't an integer (which actually is a pretty decent way of doing things).

    Likewise, there are SetInt() and such functions as well. They convert the int to a string and store it in the map.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #19
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Cat
    I have a config file reader class that a friend of mine coded. Internally it stores everything as strings, in a std::map<std::string, std::string>, where the first string is the name of the setting, and the second is the value.

    The public interface of the class, however, has functions like GetInt(std::string name) which basically does string->int conversion. How you handle failure is up to you -- you can return 0, you could throw an exception, or you could set an error flag. You could even have a second parameter which specifies the default value, which is returned if the variable doesn't exist/isn't an integer (which actually is a pretty decent way of doing things).

    Likewise, there are SetInt() and such functions as well. They convert the int to a string and store it in the map.
    Thats what I work on. Except I want to have vector string for settings since there may be more options for each.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Though implementation problem
    By Elysia in forum C++ Programming
    Replies: 296
    Last Post: 05-31-2008, 01:02 PM
  2. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2006, 12:00 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM