470,833 Members | 1,407 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Is this a shortcoming of the compiler or the C++ language?


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?

-RFH

Jun 27 '08 #1
8 1206
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.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
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?
You must not attempt to use parentheses for the default constructor when
calling the new operator. Observe:

ListOfRadioButton.push_back(new RadioButtonGroup); // does what you want

If however you are passing arguments to the constructor, you would use
the typical syntax as also seen in Java:

// also does what you want.
ListOfRadioButton.push_back(new RadioButtonGroup(arg));

--
Andrew Kerr
Jun 27 '08 #3
On Jun 3, 5:45 pm, Andrew Kerr <ark...@gatech.eduwrote:

You must not attempt to use parentheses for the default constructor
when
calling the new operator. Observe:
You just answered my next question, Andrew.

Thanks,

-Ramon

Jun 27 '08 #4
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

Jun 27 '08 #5
LR
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
Jun 27 '08 #6
Ramon F Herrera <ra***@conexus.netwrote:
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?
Yes.

Let's talk about the various signatures, this is something you simply
don't have in Java, so it will take a bit for you to get used to it.

void push_back(RadioButtonGroup&);

The signature above means that a reference of the user's object is
being passed into the function. Any changes the function makes to the
object will be reflected in the user's object directly.

void push_back(RadioButtonGroup);
void push_back(const RadioButtonGroup&);

The two above are effectively identical from the user's POV. The
difference between them lies in how the parameter is passed internally
and what can be done with the parameter once you are inside the
function. With the former, the parameter is a copy of the object that
was passed into the function and can be modified any way you wish
without affecting the user's object. With the latter, the object passed
in is a reference of the object (effectively the object itself,) and
can't be changed within the function.

void push_back(RadioButtonGroup*);
void push_back(const RadioButtonGroup*);

With the two signatures above, users can pass in an array of objects.
The number of objects passed in must be at least as many as the function
is expecting and any object passed in that the function isn't expecting
are ignored. Many times, such functions only expect an "array of one
element". In the former signature, the objects passed in can be modified
(and such changes will be seen by the user.) In the latter case, the
objects cannot be modified.

Lastly, keep track of what you new! Unlike Java, every object that is
newed must be explicitly deleted.
Jun 27 '08 #7
On Jun 3, 11:45 pm, Andrew Kerr <ark...@gatech.eduwrote:
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?
You must not attempt to use parentheses for the default
constructor when calling the new operator.
Why not?
Observe:
ListOfRadioButton.push_back(new RadioButtonGroup); // does what you want
If RadioButtonGroup has a default constructor. If
RadioButtonGroup is a POD, it creates an uninitialized
RadioButtonGroup, whereas new RadioButtonGroup() creates a
default (zero) initialized one, which is almost always
preferable.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #8
LR
James Kanze wrote:
>Do you want
std::vector< RadioButtonGroup* LORBG;
or
std::vector< RadioButtonGroup LORBG;
?
>If you want the former, can you explain the advantage you seek?

If the latter, can you explain how it's going to work if
RadioButtonGroup is not copiable
No, I can't.
>(which is usually the case for
well designed GUI components)?
I don't think that RadioButtonGroup is a GUI component, but a typedef
that the OP is making.

1) typedef std::vector<RadioButtonNode*RBG;
2) typedef std::vector<RadioButtonNodeRBG;

I think the OP in thread "Is it possible to create a vector of vector?"
asked about (2). So your question is a good one.

But I don't know what a RadioButtonNode is. So I'd still like to find
out more about what the OP intends.

LR
Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Csaba2000 | last post: by
25 posts views Thread by Mohd Hanafiah Abdullah | last post: by
5 posts views Thread by rawCoder | last post: by
18 posts views Thread by smnoff | last post: by
6 posts views Thread by toton | last post: by
41 posts views Thread by Miroslaw Makowiecki | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.