Thread: C++ style tables

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248

    C++ style tables

    Hi everybody!

    The project I'm working on contains a lot of tables, which are now defined as pointer-type arrays :
    Code:
    typedef struct {
      string name;
      double value;
      int multiplicity;
    } myParameters;
    
    static const double just_some_parameters[] =
    {
      {"one", 1.0, 1},
      {"two", 2.0, 2},
      {"three", 3.0, 1},
      {"four", 4.0, 2},
      {"five", 5.0, 3},
    };
    I would like to convert this to a more STL like array, but then I'll probably need a (static) function to initialize my tables ? I mean:
    Code:
    typedef struct {
      string name;
      double value;
      int multiplicity;
    } myParameters;
    
    static const std::vector<myParameters> = {} // will not work, of course
    Any ideas of how to get rid of pointer-type arrays when using tables ?

    Thanks for your answer!

    Mark

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Two choices:
    You write code to convert your static array into the vector.
    Write code to push_back(myParameters("one", 1.0, 1)) etc.

    --
    Matgs
    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.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Third choice: leave this stuff as it is. There's absolutely nothing wrong with it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    OK, thanks !

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    Third choice: leave this stuff as it is. There's absolutely nothing wrong with it.
    I fully agree with that.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Which style of if loops is most common?
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 08-25-2005, 03:18 PM
  4. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 11:21 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM