Ramon F Herrera wrote:
On Jun 3, 5:41 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>Ramon F Herrera wrote:
>>I have some code like this:
RadioButtonGroup *empty_vector = new RadioButtonGroup();
ListOfRadioButton.push_back(*empty_vector);
Coming from the Java camp, I keep on trying to write the above as
follows:
ListOfRadioButton.push_back(new RadioButtonGroup());
but my compiler (MSVC++) doesn't allow me. Is that behavior sanctioned
by the standard?
What behaviour? Not allowing you to violate the type requirement? Your
'ListOfRadioButton' is likely a list of *instances*. You're trying to
stick a pointer there. Make your 'ListOfRadioButton' contain the actual
pointers, and the compiler will let you. So, your code should be either
RadioButtonGroup *empty_vector = new RadioButtonGroup();
ListOfRadioButton.push_back(empty_vector); // NO ASTERISK!!!
or
ListOfRadioButton.push_back(new RadioButtonGroup());
You could, of course, do
ListOfRadioButton.push_back(* (new RadioButtonGroup()));
or simply
ListOfRadioButton.push_back(RadioButtonGroup());
(no need for 'new'). I guess you need to unlearn some habits Java
pushed on you, like the desire to stick 'new' all over the place.
Victor:
Thank you very much for your recommendations. Some of the suggested
statements you propose are not being accepted by the compiler. I will
post a full example, because this is driving me nuts and would like to
make some sense out of it. What I have so far is black box magic. I
should try gcc as well.
-Ramon
BTW, should you insist on doing something like
typedef std::vector<RadioButtonRadioButtonGroup;
std::vector< RadioButtonGroup* ListOfRadioButtonGroup;
and then
ListOfRadioButtonGroup.push_back( new RadioButtonGroup );
remember to delete everything in ListOfRadioButtonGroup before it goes
out of scope. Because unlike,
typedef std::vector<RBRBG;
std::vector< RBG LORBG;
and then
LORBG.push_back( RBG() );
the code with pointers may result in memory leaks, if you don't delete
what you newed, either by doing it yourself explicitly, or by use of
some smart pointer. If you do it yourself, then beware exceptions that
might take you out of scope.
But what is it you're trying to do?
Do you want
std::vector< RadioButtonGroup* LORBG;
or
std::vector< RadioButtonGroup LORBG;
?
If you want the former, can you explain the advantage you seek?
Perhaps the instances of RBs already exist on the stack or the heap and
all you want to do is associate them with pointers in these vectors?
But that would probably be something like..
RB aButton, bButton, cButton, dButton, eButton;
std::vector< std::vector< RB* LORBG;
std::vector<RB*group1, group2;
group1.push_back(&aButton);
group1.push_back(&bButton);
group2.push_back(&cButton);
group2.push_back(&dButton);
group2.push_back(&eButton);
LORGB.push_back(group1);
LORGB.push_back(group2);
and now you don't have to worry about deleting stuff, because that will
happen when you go out of scope.
But I still feel that I don't understand what it is you're trying to
accomplish. Perhaps you could expand on that?
LR