1

EDIT: This structure is real bad design, don't use it!

Is it possible to initialize a list of pointers to object (initializing everything in the same time) in C++11?

For example, let's say I want to create my_list which is of type:

list<pair<string, map<string, string> *> *>.

I'd like to initialize it with raw values.

list<pair<string, map<string, string> *> *> my_list = { ??? }

???: creates new pointers to new pairs of raw strings and new pointers to maps of raw strings.

(Why I need to do this: this data structure is storing a configuration read from a file but if the file is not found I need a default value in my code)

valentin
  • 2,596
  • 6
  • 28
  • 48

2 Answers2

5

Yes, sure:

list<pair<string, map<string, string> *> *> x {
    new pair<string, map<string, string> *>  {
        "hello", new map<string, string> { {"a", "1"}, {"b", "2"} } },
    new pair<string, map<string, string> *> {
        "world", new map<string, string> { {"a", "1"}, {"b", "2"} } },
};

This is awful design. Don't do this.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    That last part needs to be big, bold, and in some stand-outish color. :P – cHao Apr 01 '14 at 05:01
  • 2
    @cHao: I'll change it for 35 Unicoins. – Kerrek SB Apr 01 '14 at 05:02
  • :p thanks, I won't do this but it's interesting to know how to do it ^_^ – valentin Apr 01 '14 at 05:03
  • @toogy: A `list>>` might be OK, or perhaps something more appropriate for the actual problem at hand... – Kerrek SB Apr 01 '14 at 05:04
  • @KerrekSB: what are you thinking about? This is just to store the content of a simple config file with multiple `[section]` and some `key=value` inside the section. (I have to use a list because sections can have the same name) – valentin Apr 01 '14 at 05:09
  • @toogy: How about `map>` or `multimap`? – Kerrek SB Apr 01 '14 at 05:10
  • @KerrekSB: Sorry just edited my last comment. I have to use a list because sections can have the same name. – valentin Apr 01 '14 at 05:10
  • @toogy: Seems a bit odd, unless the two sections are related to the point where they could be merged anyway. It sounds like you're trying to nest sections, which .INI files kind suck at doing. Try instead looking at how .REG files handle this issue -- the format is quite similar at its core to .INI, but section names include the full key. – cHao Apr 01 '14 at 09:55
1

Old post, but may be useful You can try something like this in C, C++ is very similar

Typedef struct {
   char *name;
   int   data;
   struct MyList *next;
} MyList;

Mylist junk[] = {
 {"One"  ,1, &junk[1]},
 {"Two",  2, &junk[2]},
 {"Three",3, (MyList *) 0}
};

MyList *head = &junk[0];

is a pointer to the head of the list.....

These lists are very useful and common in small embedded interpereters. Lisp and Forth come immediately to mind. and many other places.

Look at stackflow discussion self referential struct definition?

Doing list surgery may be an issue since the compiled list may live in non volatile memory - though in more modern CPU this is probably in flash.

As you can see in the above post there's nothing inherently bad in lists, they're just tricky to use and methods to use them are not taught as rigorously as in the past.

The awful syntax you often try (e.g. ) is simply an artifact of C/C++ languages which was not designed to enable easy representation of lists of pointers in code.

You can also try something of the form

MyList a = {"A",(MyList *) 0};
MyList b = {"B",&a);

Another common pattern is to do the initialization programatically

MyList   *m = NewMyList("A");
MyListAdd(m,"B");

You find a lot of this in Java where there are good container class libraries. Downside is that your startup may take a few extra milliseconds.

Another common usage is to use relative links .... in the array example above, replace the link by sizeof(MyList). This makes for easier programming plus the list becomes relocatable
and can be copied (eg from FLASH to RAM and back) using standard memcpy type operations.

have fun Chris

Community
  • 1
  • 1
ChrisR
  • 812
  • 5
  • 10