multiple 'new' at single line

G

Grizlyk

Hi, people.

Can anybody explain me "multiple 'new' at single line"
behavior. Consider:

p::p(void*);
p::p(void*,void*);

new A( p(new B), p( new C(p(new D), p(new E)) ), p(new F));

Will it be either

1.
new F p(*)
new E p(*)
new D p(*)
new C p(*)
new B p(*)
new A

and
2.
new B p(*)
new D p(*)
new E p(*)
new C p(*)
new F p(*)
new A p(*)

or
3.

new E
new D
new F
new C
new B
p(*)
p(*)
p(*)
p(*)
p(*)
new A

Does C++ guarantee that never can be like upper
case 3 (more than one "new" befor "p(*)").

Do we need to create temp storages:

const p tmp1(new F);
const p tmp2(new E);
const p tmp3(new D);
const p tmp4(new C(tmp3,tmp2));
const p tmp5(new B);
new A(tmp5,tmp4,tmp1);
 
D

Daniel T.

Grizlyk said:
Hi, people.

Can anybody explain me "multiple 'new' at single line"
behavior. Consider:

p::p(void*);
p::p(void*,void*);

new A( p(new B), p( new C(p(new D), p(new E)) ), p(new F));

The only order guarantee you have with the above is that the B, D, E and
F object will be created first (in any order); that C will be created
after D and E; and that B through F will all be created before A.
Outside of that, there are no order guarantees.
 
G

Grizlyk

Daniel said:
The only order guarantee you have with the above is that the B, D, E and
F object will be created first (in any order); that C will be created
after D and E; and that B through F will all be created before A.
Outside of that, there are no order guarantees.

I do not understand, can be optimized or no like this:

compiled_tmp1=new D,
compiled_tmp2=new E,
compiled_p1=p(compiled_tmp1),
compiled_p2=p(compiled_tmp2),
new c(compiled_p1, compiled_p2);
 
D

Daniel T.

Grizlyk said:
I do not understand, can be optimized or no like this:

compiled_tmp1=new D,
compiled_tmp2=new E,
compiled_p1=p(compiled_tmp1),
compiled_p2=p(compiled_tmp2),
new c(compiled_p1, compiled_p2);

Yes, it can be done like that. It can also be done like this:

compiled_tmp1=new D,
compiled_p1=p(compiled_tmp1),
compiled_tmp2=new E,
compiled_p2=p(compiled_tmp2),
new c(compiled_p1, compiled_p2);

or like this:

compiled_tmp2=new E,
compiled_p2=p(compiled_tmp2),
compiled_tmp1=new D,
compiled_p1=p(compiled_tmp1),
new c(compiled_p1, compiled_p2);

or like this:

compiled_tmp2=new E,
compiled_tmp1=new D,
compiled_p1=p(compiled_tmp1),
compiled_p2=p(compiled_tmp2),
new c(compiled_p1, compiled_p2);

Or any of a few other orders I expect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top