Afgesplitst van De Devschuur Coffee Corner - Iteratie 3
Is de ?: operator echt de enige manier om conditioneel een reference te initialiseren?
C++:
| 1
2
3
4
5
6
 | int& foo; if ( boolean ) foo = bar; else foo = baz; | 
Werkt niet omdat: een reference dient altijd geïnitialiseerd te zijn.
Wat dacht je dan van
C++:
| 1
2
3
4
 | if ( boolean ) int& foo = bar; else int& foo = baz; | 
Nee, dat werkt ook niet. Want na de if raakt foo uit scope..
Mag ik u voorstellen aan deze oude vriend?
C++:
| 1
2
3
4
5
6
7
8
9
10
 | if ( boolean ) goto next; int& foo = baz; goto finish; next: int& foo = bar; finish: | 
Foo already initialized. Je meent het?
Dan maar zo:
C++:
| 1
 | int& foo = boolean ? bar : baz; | 
al is dat bij ingewikkeldere constructs natuurlijk totaal onleesbaar.
[ Voor 5% gewijzigd door Woy op 13-05-2013 11:06 ]
:strip_icc():strip_exif()/u/12461/crop65b195553d948.jpg?f=community) 
            :strip_icc():strip_exif()/u/77332/crop5e54ec42433bc_cropped.jpeg?f=community) 
            /u/126369/Daos2.png?f=community) 
            :strip_exif()/u/73955/foxavatar-60.gif?f=community) 
            /u/142011/crop65b383c6c6c2f_cropped.png?f=community) 
            :strip_icc():strip_exif()/u/35767/images.jpg?f=community) 
            /u/27299/hoofd.png?f=community)