pointers to elements in containers



I have a class that looks like that:

struct Detail {
std::list<const std::string> fields;
std::list<const std::string*> key_fields;
};

The 'key_fields' container is meant to store pointers to the std:string's in
'fields', so Detail's constructor looks like the following:

Detail::Detail()
{
fields.push_back(std::string("string 1"));
key_fields.push_back(&fields.back());

fields.push_back(std::string("string 2"));
key_fields.push_back(&fields.back());

//...
}

This does not work: pointers to elements in 'fields' point to valid
locations only until the end of the constructor after which they change (if
addresses of elements stored in 'fields' were to be taken once the
constructor has completed). (I have tried variations on the same theme but
whatever the container - list or vector - and whatever the method of
obtaining the address the underlying principle seems to remain the same:
addresses of elements in a container storing objects change once the
constructor has completed.)

Working on the above, I assumed the following:

1) Addresses of the elements in a vector may change after resize(),
push_back(), insert(), erase() - perhaps a few others of the similar
nature - but if you take care and call resize() or reserve() beforehand and
then assign (through []), they should not. (Initially I tried it with a
vector, although later realised having a vector was not the issue.)

2) Lists (and maps) are not affected by (1), as far as elements other than
those immediately affected by the operation are concerned.

Could it be that real copies are created upon the exit from the constructor?
How is this generally defined?

(The reason why I have two containers above is because not all of the fields
required are actually keys - supposed to be unique and to contain data - so
I thought I would store keys separately for ease of manipulation.)

Thank you.


.



Relevant Pages

  • Re: pointers to elements in containers
    ... The 'key_fields' container is meant to store pointers to the ... so Detail's constructor looks like the ... pointers to elements in 'fields' point to valid ... Is your Detail struct being copied? ...
    (microsoft.public.vc.language)
  • Re: default values - the good, the bad and the ugly
    ... > The thing is that in your tale, they didn't change only a constructor ... In the specific case the someClass is a container of ... geometric curves, 'd' is the c0 continuous tolerance between ... the capacity reserved would be double the last size. ...
    (comp.lang.cpp)
  • Re: Equivalent to VCLs DataModule
    ... components if it implements System.ComponentModel.Component's constructor which takes a System.ComponentModel.IContainer as a parameter? ... a constructor taking IContainer and calling IContainer.Add, so that if you for example drag drop your component onto a form, VS ensures that your component will be added to the form's Container object. ... Does the IDE know to look for a private variable of type System.ComponentModel.Container in a CLR object in order to have that object act as a container for other objects at design time? ...
    (microsoft.public.dotnet.framework)
  • Re: [std::vector] operator[], the heap and try-carch blocks
    ... However, if the container contains ... You should be able to get away without their use by setting data members to ... complicated resource acquisition in the constructor body. ... thereby allowing you to complete object construction ...
    (alt.comp.lang.learn.c-cpp)
  • Re: custom Swing component
    ... >i want to create a custom component with a text field and a button. ... >using netbeans IDE.when i place it to a container and resize the ... Now instantiate an instance of the ChanchalPanel and add it to some ...
    (comp.lang.java.gui)