0

I am currently defining some constants:

struct ModInfo {
    int numChoices;
    string menu;

    ModInfo (int count, string menuText) : numChoices(count), menu(menuText) {
    }
};

const ModInfo menus[4] = {ModInfo(3, "..."), ModInfo(7, "...", ...};

Each ModInfo contains the information for a module that will be used by main() in printing menus to the display and flow control to determine which module's menu to print and which function in it to subsequently execute.

Instead of accessing a module's info inside an array element via array index, can I assign an identifier (ie. the module's name) to that element instead? Is there a direct way to do this, or is enum the only hack to do this?

Edit: Please stop asking me to use C++11 in every question I ask. I would if I could.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
thegreatjedi
  • 2,788
  • 4
  • 28
  • 49
  • 1
    I tagged as C++03. You should use this tag if you're restricted to pre-C++11 dialects. – juanchopanza Jan 07 '16 at 07:23
  • Wrong terminology: identifier usually refer to tokens in the source code. – Basile Starynkevitch Jan 07 '16 at 07:24
  • @BasileStarynkevitch It's not wrong terminology. I am asking if array elements can be mapped to an identifier during development, not a string value that will persist into the build. – thegreatjedi Jan 07 '16 at 07:25
  • 1
    @thegreatjedi: If you want to use an identifier, then I don't see why enums are a hack. Could you include in your question some pseudocode that describes what you'd like to do? – Bill Lynch Jan 07 '16 at 07:26
  • Is it sufficient to have the module identifier be an actual C++ identifier (e.g. a constant), or do you need something you can specify at runtime, e.g. a string? – Frerich Raabe Jan 07 '16 at 07:28
  • @BillLynch There's not much more to it. Any use is just accessing the array. I just want to give a name to the elements I'm accessing instead of index numbers for readability. Wouldn't enums be considered kinda like a crude way of getting it done, or is it considered an appropriate design? – thegreatjedi Jan 07 '16 at 07:29
  • @FrerichRaabe I only want something more readable than index numbers during development. – thegreatjedi Jan 07 '16 at 07:31

4 Answers4

1

Rather than being a hack, an enum is the correct tool for your use case.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
1

You could set up your array using an X macro:

#define MODULES \
  X(MenuItemA, 24, "some menu text") \
  X(AnotherMenuItem, 99, "here's another menu item" )

Then, you define both the array of menus as well as an enum with descriptive constants from the same table by defining X appropriately:

#define X(id, count, text) ModInfo(count, #text),
const ModInfo menus[] = {
  MODULES,
  ModInfo(-1, "")
};
#undef X

#define X(id, count, text) id,
struct ModuleId {
   enum Value {
MODULES
   };
};
#undef X

You could then access your module information via e.g.

menus[ModuleId::AnotherMenuItem].text;
Community
  • 1
  • 1
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
0

Please use C++11 (perhaps even as a tag). You might use some recent version of GCC or of Clang/LLVM (perhaps by compiling a compiler from its source code), both are free software.

(versions of C++ before C++11 are obsolete; and the current standard is C++14; if you need an old version of C++ you should tag appropriately your question)

You should learn about C++ containers and smart pointers and you might use std::map<std::string,std::unique_ptr<ModInfo>> to associate to some (name) string a pointer to your ModInfo

Read a good book about Programming using C++; BTW reading SICP and practicing some Scheme won't harm.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • C++11 is not available to me in my environment. Secondly, I am not looking for a way to map a string to the element. I'm looking for a way to map identifiers (variable names) to the element. – thegreatjedi Jan 07 '16 at 07:16
  • Then upgrade your compiler to some more recent version. – Basile Starynkevitch Jan 07 '16 at 07:17
  • 2
    Upgrading isn't an option. The version is determined by the organization and has to follow conventions set by partner institutions. – thegreatjedi Jan 07 '16 at 07:19
  • Then you should have tagged your question with the obsolete version of C++ that you need to use. I am sorry for you. IIRC, `std::map` existed in some old versions of C++ – Basile Starynkevitch Jan 07 '16 at 07:20
  • 1
    @BasileStarynkevitch I can't see why C++11 is needed at all (given that I can't see why unique_ptr is needed at all.) – juanchopanza Jan 07 '16 at 07:21
  • @BasileStarynkevitch Really? I wouldn't have known whether the solution I seek is only available in C++11 or not. My question is not a C++11-specific issue, nor is it a C++03-specific issue, why does it necessarily need a C++xx tag instead of C++? – thegreatjedi Jan 07 '16 at 07:23
  • 1
    @thegreatjedi Because C++ is now C++14, which is includes the C++11 functionality. – juanchopanza Jan 07 '16 at 07:24
0

Perhaps then a structure with named fields would be more convenient if the size of the array is not too big.

struct MyNamedStruct
{
    int NamedVariable1;
    int NamedVariable2;
    ...
    int NamedVariableX;
};

MyNamedStruct a;
...
a.NamedVariable1 = 10;
dmi
  • 1,424
  • 1
  • 9
  • 9