-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathclass.cpp
1696 lines (1308 loc) · 48.7 KB
/
class.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
# class
TODO split this up.
*/
#include "common.hpp"
class Empty {};
/**
This class has a compiler supplied default constructor.
*/
class ImplicitDefaultCtor {
public:
int i;
std::string s;
};
/**
This class has no default constructor since another constructor was defined.
*/
class NoDefaultCtor {
public:
NoDefaultCtor(int i) : i(i) {}
int i;
};
/**
This class defines a default constructor since it will also provide a non default one.
*/
class ExplicitDefaultCtor {
public:
int i;
ExplicitDefaultCtor(){}
ExplicitDefaultCtor(int i) : i(i){}
};
/**
This class uses its default copy constructor and assign operator.
*/
class DefaultCopyAssignCtor {
public:
int i;
DefaultCopyAssignCtor() : i(0) {}
DefaultCopyAssignCtor(int i) : i(i) {}
};
/**
This politically incorrect clas does not implement the equality == operator.
*/
class NoEquality {
public:
NoEquality() : i(0) {}
int i;
};
#if __cplusplus >= 201103L
class CtorFromCtor {
public:
std::vector<int> v;
/**
This constructor does the default which is calling the parent's default constrctor before its own.
An error would be generated if the parent class has no defalt constructor.
*/
CtorFromCtor(int i) : v(1,i) {}
/**
This constructor calls another constructor with arguments before running its own.
*/
CtorFromCtor(int i, int j) : CtorFromCtor(i) {
v.push_back(j);
}
};
#endif
#if __cplusplus >= 201103L
/**
This class exemplifies the `= default` syntax.
*/
class DefaultKeyword {
public:
/**
This is an explicitly defaulted constructor.
*/
DefaultKeyword() = default;
/**
This is an explicitly defaulted copy constructor.
*/
DefaultKeyword(const DefaultKeyword&) = default;
/**
This is an explicitly defaulted assignment operator.
*/
DefaultKeyword & operator=(const DefaultKeyword&) = default;
~DefaultKeyword() = default;
// ERROR: Not possible because the default constructor must take no arguments.
//DefaultKeyword(int i) = default;
// With the default keyword it is possible to have other
// constructors besides the Implicitly defined constructor.
DefaultKeyword(int i, std::string s) : i(i), s(s) {}
/**
ERROR: Cannot default anything other than:
- constructors
- destructors
- copy
- assignment
*/
//void f() = default;
int i;
std::string s;
};
/**
This class exemplifies the `= delete` syntax.
*/
class DeleteKeyword {
public:
DeleteKeyword() = delete;
DeleteKeyword(int i) : i(i) {}
DeleteKeyword(const DeleteKeyword&) = delete;
DeleteKeyword & operator=(const DeleteKeyword&) = delete;
/**
With this, the code would compile, but it would not be possible
to create any instances of this class, since at some point the destructor
would have to be called!
*/
//~DeleteKeyword() = delete;
/* It is possible to delete any function. */
void f() = delete;
int i;
};
#endif
/**
This class has an implicit default constructor.
*/
class UniformInitializationImplicitCtor {
public:
int i;
int j;
};
/**
This class has an explicit default constructor,
and no constructor that takes 2 ints.
*/
class UniformInitializationExplicitCtor {
public:
int i;
int j;
UniformInitializationExplicitCtor() : i(0), j(0) {}
};
/**
This class has a constructor that takes 2 ints.
*/
class UniformInitializationCtor2 {
public:
int i;
int j;
UniformInitializationCtor2(int i, int j) : i(i), j(j+1) {}
bool operator==(const UniformInitializationCtor2& other) {return this->i == other.i && this->j == other.j;}
};
int UniformInitializationCtor2Func(UniformInitializationCtor2 o){
return o.i + o.j;
}
class UniformInitializationList {
public:
int i;
int j;
UniformInitializationList(int i, int j) : i(i), j(j+1) {}
UniformInitializationList(std::initializer_list<int> list){
i = *(list.begin());
j = *(list.begin() + 1);
}
};
class MemberConstructorTest {
public:
NoBaseNoMember0 member0;
NoBaseNoMember1 member1;
MemberConstructorTest(){callStack.push_back("MemberConstructorTest::MemberConstructorTest()");}
~MemberConstructorTest(){callStack.push_back("MemberConstructorTest::~MemberConstructorTest()");}
void method(){callStack.push_back("MemberConstructorTest::method()");}
};
class Member {
public:
Member(){callStack.push_back("Member::Member()");}
Member(int i){callStack.push_back("Member::Member(int)");}
~Member(){callStack.push_back("Member::~Member()");}
void method() {callStack.push_back("Member::method()");}
int i;
};
class Nested {
public:
Nested() { callStack.push_back("Nested::Nested()"); }
};
/*
# this
Magic value that points to the current object.
It is implemented by the compiler by passing `this` as the first argument
of every non-static function of the class.
This is noticeable when doing operator overload:
*/
class Base {
public:
/// Best to put typedefs on top of class so def will go for entire class.
typedef int NESTED_INT;
Base() : i(0), j(1) {
callStack.push_back("Base::Base()");
// BAD: same as list init, except if i is an object
// to keep uniform style, always use list init
//this->i=0;
//this->j=1;
// ERROR: ic is const. must be initialized in list initialization.
//ic=0;
// BAD: compiles but infinite loop!
//Base b;
}
/*
# Initialization list
Initialization lists have 4 main uses:
1) avoid calling member object constructor and copy separately
https://stackoverflow.com/questions/12927169/how-can-i-initialize-c-object-member-variables-in-the-constructor
2) initializing base classes with non default constructors
3) initializing const elements
4) initializing member references &
5) C++11 delegating constructors: call another constructor of the current class
*/
Base(int i, int j) : i(i), j(j) {
callStack.push_back("Base::Base(int, int)");
}
// ERROR: constructor cannot be virtual:
//virtual Base(float f){}
#if __cplusplus >= 201103L
/// C++11 initialize array/std containers in list initializtion.
Base(float f) : i(0), fs4{f,f,f,f}, vi{0,1,2,3} {
callStack.push_back("Base::Base(float)");
}
#endif
virtual ~Base() {
callStack.push_back("Base::~Base()");
}
void method() {
callStack.push_back("Base::method()");
int i = iAmbiguous;
}
void constMethod() const;
void constMethod(int *&) const;
void methodAmbiguous(){callStack.push_back("Base::methodAmbiguous()");}
// virtual: decides on runtime based on object type
// http://stackoverflow.com/questions/2391679/can-someone-explain-c-virtual-methods
virtual void virtualMethod(){callStack.push_back("Base::virtualMethod()");}
int i, j;
// ERROR: cannot initialize here.
//int initialized_outside_ctor = 0;
int iPublic;
int iAmbiguous;
int* is;
float fs4[4];
std::vector<int> vi;
mutable int mutableI;
// ERROR: statics can be changed in const functions by default.
//static mutable int staticMutableI;
/*
BAD every class must have an assigment operator.
But then, assigment does something like this->ic = other->ic
You could redefine the assigment, but still in your new definition ic cannot be changed.
<http://stackoverflow.com/questions/634662/non-static-const-member-cant-use-default-assignment-operator>
*/
/*
# member initialization outside of constructor
*/
const int iConstInit = 0;
// ERROR: non-integral type.
//const static float fConstStatic = 0.0;
const static Member member;
// OK: why ok?
const static Member member2;
//default constructor works
//const static NoBaseNoMember(1);
/*
ERROR: non integral type must be init outside.
why integral types are an exception:
<http://stackoverflow.com/questions/13697265/static-const-double-cannot-have-an-in-class-initializer-why-is-it-so>
*/
class Nested {
public:
Nested() {
callStack.push_back("Base::Nested::Nested()");
int i = privateStaticInt;
//you have private access
}
Member m;
};
class Nested2 {
public:
Nested2() {
callStack.push_back("Base::Nested2::Nested2()");
}
Nested innerIn;
//inner one
::Nested innerOut;
//outter one
};
protected:
int iProtected;
void fProtected(){callStack.push_back("Base::fProtected()");}
private:
int iPrivate;
void fPrivate(){callStack.push_back("Base::fPrivate()");}
const static int privateStaticInt = 1;
typedef int PRIVATE_NESTED_INT;
};
/*
# friend
Allow external functions and other classes to access private memebers of this class.
Friendship is not automatically reflexive nor transitive.
One case in which friendship may be unavoidable is for operator overload of operators which cannot
be class members and must be implemented as external functions such as `operator<<`.
This happens because of the nature of operators, which may force them to be implemented outside the class.
<http://www.cplusplus.com/doc/tutorial/inheritance/>
# friend and templates
Things get complicated when friends and template classes interact:
<http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Ffriends_and_templates.htm>
*/
class FriendOfFriend;
class Friend {
public:
friend class FriendOfFriend;
Friend(int i) : i(i) {}
int getI(){return this->i;}
//this declaration says that `friendGetIPrivate(Base)` is a friend of this class.
//It will be defined outside the class.
friend int friendGetI(Friend f);
/* The same as friendGetI, but also defined inside the class. */
friend int friendGetIInnerDefine(Friend f) {
// ERROR: it is as if this were a friend external function, so there is no `this`.
//return this->i;
return f.i;
}
int getFriendI(FriendOfFriend f);
private:
int i;
void privateMethod(){};
};
/* cannot use the word friend here */
int friendGetI(Friend f){
// ERROR: this is a non-member function, so no `this`.
//return this->i;
return f.i;
}
class FriendOfFriend {
public:
FriendOfFriend(int i) : i(i) {}
int getFriendI(Friend f){return f.i;}
void callFriendPrivateMethod(Friend f){f.privateMethod();}
private:
int i;
};
// ERROR: friend used outside class.
//friend int friendGetI(Friend f){return f.i;}
// ERROR: not a friend because reflexivity is not automatic.
//int Friend::getFriendI(FriendOfFriend f) {return f.i;}
/*
# const method
Methods that cannot change the data of their object.
Inner workings: the hidden *this* pointer is downgraded to `const X*` when passing it to the function:
void method() const
becomes:
void method(const Class* this) const
instead of:
void method(Class* this) const
*/
void Base::constMethod() const {
// ERROR: cant assign member in const func.
//this->i = 2;
// ERROR: cant call non const method inside const method!
// as that method could change the object
//this->member.method();
// ERROR: cant assign member of a member in const method.
//this->member.i = 1;
/*
# mutable
OK
Mutable allows you to change it even in a const method.
Application to multithreading:
http://stackoverflow.com/questions/105014/does-the-mutable-keyword-have-any-purpose-other-than-allowing-the-variable-to
*/
this->mutableI = 1;
callStack.push_back("Base::constMethod()");
{
// ERROR: conversion from const Base* to Base*.
//Base* this2 = this;
const Base* this2const = this;
}
}
// ERROR must not ommit the const here either.
//void Base::constMethod () {}
/*
Cannot return a non const pointer from a const method, since this is const,
so all members are also const.
*/
void Base::constMethod(int *& ip) const {
// ERROR: invalid conversion.
//ip = &this->iPrivate;
}
// Must come outside
const Member Base::member2 = Member(1);
// ERROR: must be declared inside.
//int Base::k;
class BaseProtected {
public:
BaseProtected(){callStack.push_back("BaseProtected::BaseProtected()");}
BaseProtected(int i){callStack.push_back("BaseProtected::BaseProtected(int)");}
~BaseProtected(){callStack.push_back("BaseProtected::~BaseProtected()");}
};
class BasePrivate {
public:
BasePrivate(){callStack.push_back("BasePrivate::BasePrivate()");}
BasePrivate(int i){callStack.push_back("BasePrivate::BasePrivate(int)");}
~BasePrivate(){callStack.push_back("BasePrivate::~BasePrivate()");}
};
class Derived : private BasePrivate {};
class Class :
public Base,
// ERROR: duplicate.
//public Base,
// WARN: cannot use BasePrivate inside: ambiguous.
//public Derived,
protected BaseProtected,
private BasePrivate
{
public:
/*
Call base constructors first.
*/
Class() : i(0), z(1) {
callStack.push_back("Class::Class()");
}
Class(int i) : i(i), z(0) {
callStack.push_back("Class::Class(int)");
}
/*
Calls specific base constructors instead of default ones
another application os initialization lists.
*/
Class(int i, int z) : Base(i,z), BaseProtected(i), BasePrivate(i), i(i), z(z) {
callStack.push_back("Class::Class(int, int)");
}
// WARN: BaseAbstract will be init after TODO ?
//Class() : BaseAbstract(), Base(i,z), BaseProtected(i), BasePrivate(i), i(i), z(z)
//try catch in case base constructor can throw exceptions
Class(int i, int j, int z) try : Base(i,j), i(i), z(z) {
callStack.push_back("Class::Class(int, int, int)");
}
catch(const std::exception &e) {
throw e;
}
Class(Member m) : m(m) {
// BAD: m constructor would be called, but this is useless since we have already called it!
// to construct it before.
// This is an application of initialization constructors.
//this->m = m;
callStack.push_back("Class::Class(Member)");
}
/*
Copy constructor.
Classes already have this by default.
Useful to customize if class does dynamic allocation!
*/
Class(const Class& c) : i(c.i), z(c.z), m(c.m) {
callStack.push_back("Class::Class(Class)");
}
// Classes don't have constructors from base by default.
Class(const Base& b) : Base(b) {
callStack.push_back("Class::Class(Base)");
}
/*
Also calls Base destructor after
*/
~Class() { callStack.push_back("Class::~Class()"); }
// Called method overwriding.
void method() { callStack.push_back("Class::method()"); }
// OK.
template<class C=int>
void methodTemplate() {
callStack.push_back("Class::methodTemplate()");
}
int i;
int z;
Member m;
Nested nested;
//Base nested class visible from here
};
//nested
// OK: you can see the nested class from derived classes
class NestedDerived : Class::Nested{};
class Class2 : public Base {
public:
// OK: you can override the Nested class from the Base also
class Nested{};
};
class ClassCast {
ClassCast(Class c){}
};
// ERROR:
//ClassDefault::ClassDefault(int i=0){}
/*
Illustrates the copy and swap idiom and related concepts like move contruction.
*/
class CopyAndSwap {
public:
int *is;
std::size_t n;
CopyAndSwap(std::size_t n, int val) : n(n) {
is = new int[n];
for (std::size_t i = 0; i < n; ++i) {
is[i] = val;
}
}
~CopyAndSwap(){
delete[] is;
}
CopyAndSwap& operator=(const CopyAndSwap& rhs) {
delete[] is;
is = new int[rhs.n];
return *this;
}
CopyAndSwap(const CopyAndSwap& other) {
}
};
// Design patterns
// VisibleInnerIterable
/*
this is the best way I could find to make a member
iterable object such as a container available outside
design goal:
- to change container type, you only change a single typedef
difficulty:
- there is no `Iterator` interface that iterates over anything in the stdlib
for performance reasons.
By iterable understand something that has an `::iterator`,
a `begin()` and an `end()` methods, like stl containers
*/
class VisibleInnerIterable {
public:
VisibleInnerIterable();
typedef std::vector<int> Iterable;
const Iterable& getIterable();
private:
Iterable iterable;
};
VisibleInnerIterable::VisibleInnerIterable() : iterable{0,1,2} {}
const VisibleInnerIterable::Iterable& VisibleInnerIterable::getIterable() {
return iterable;
}
int main() {
#if __cplusplus >= 201103L
/*
define class inside function
*/
{
// As of C++11, classes can be defined inside functions!
// This matches the behaviour for structs in C.
{
class C {};
}
// Template classes however cannot be defined in functions.
// Just think what should the compiler do in this case?
{
//template<class T> class C {};
}
}
#endif
/*
# Constructor
Called whenever object is created to initialize the object.
*/
{
/*
# Default constructors
http://en.cppreference.com/w/cpp/language/default_constructor
Constructor that takes no arguments.
Can be one explicitly created by the programmer:
all that matters is that it takes no arguments.
It is a good idea to always implement a default constructor,
since this is the only way arrays of fixed numbers of objects can be created before C++03
The concept of default constructor exists because there are
certain differences in syntax and behaviour when using default constructors,
for example:
- the most vexing parse only happens for default constructors.
- the default constructor can be is implicitly declared and defiend by the compiler.
There are other functions which are also defined by default:
https://en.wikipedia.org/wiki/Special_member_functions
# Implicitly declared constructors
If no explicit constructor is declared,
the compiler automatically declares the following functions:
- default constructor
- copy constructor
- assignment constructor
- destructor
If any constructor is explicitly declared, even one taking multiple default args,
then *none* of the implicitly declared constructors are declared: that must be done manually.
# Trivial default constructor
# Implicitly defined default constructor
If the implicitly-declared default constructor is not deleted
or trivial, it is defined (that is, a function body is generated
and compiled) by the compiler, and it has exactly the same effect
as a user-defined constructor with empty body and empty initializer list.
That is, it calls the default constructors of the bases
and of the non-static members of this class.
In particular, the IDDC does not do zero initialization on non-class members
such as `int`, so those have undefined values.
*/
{
// To call the default constructor, use this syntax
{
callStack.clear();
NoBaseNoMember c; //default constructor was called!
std::vector<std::string> expectedCallStack = {
"NoBaseNoMember::NoBaseNoMember()",
};
assert(callStack == expectedCallStack);
}
/*
The implicitly defined default constructor does not necessarily initialize member built-in types:
http://stackoverflow.com/questions/2417065/does-the-default-constructor-initialize-built-in-types
Class member default constructors however are called.
*/
{
ImplicitDefaultCtor o;
if (o.i != 0)
// undefined behaviour.
std::cout << "ImplicitDefaultCtor undefined behaviour: o.i = " << o.i << std::endl;
// Defined behaviour because class:
assert(o.s == "");
// *however*, the following does value initialization,
// not the default constructor, and built-in members are indeed 0 initialized!
{
ImplicitDefaultCtor o = ImplicitDefaultCtor();
assert(o.i == 0);
}
}
{
// ERROR:
//NoDefaultCtor o;
// ERROR cannot be done because this class has not default constructors.
//NoDefaultCtor os[2];
// Implicit copy and assign are still defined:
NoDefaultCtor o(1);
NoDefaultCtor o2(o);
o = o2;
}
#if __cplusplus >= 201103L
/*
# Default keyword
As of C++11, the `= default` statement can be added to a constructor to explicitly
say that the default should be used.
This allows a class to have multple constructors,
plus the implictly defined one.
*/
{
DefaultKeyword o;
// Undefined behaviour.
//assert(o.i == 0);
assert(o.s == std::string());
o = DefaultKeyword(1, "a");
assert(o.i == 1);
assert(o.s == "a");
}
/*
# delete keyword
*/
{
// ERROR: Explicitly deleted:
//DeleteKeyword o;
DeleteKeyword o(1);
// ERROR: Explicitly deleted:
//DeleteKeyword o2(o);
DeleteKeyword o2(2);
// ERROR: Explicitly deleted:
//o2 = o1;
}
#endif
}
/*
Value initialization and zero initialization are both a bit subtle,
so it is best not to rely on them.
C++ has 3 types of init
# Default initialization
# Value initialization
# Zero initialization
- http://en.cppreference.com/w/cpp/language/value_initialization
- http://en.cppreference.com/w/cpp/language/zero_initialization
- http://stackoverflow.com/questions/1613341/what-do-the-following-phrases-mean-in-c-zero-default-and-value-initializat
*/
{
// Syntax with new.
{
// Base types.
{
int* is = new int[2]();
assert(is[0] == 0);
assert(is[1] == 0);
delete[] is;
}
// Works for structs.
{
struct T {int a;};
T *t = new T[1]();
assert(t[0].a == 0);
delete[] t;
}
// Works for objects.
//
// Note how the default constructor was called since `z == 1`.
{
{
Class *cs = new Class[1]();
assert(cs[0].i == 0);
assert(cs[0].z == 1);
delete[] cs;
}
}
// But only works with default constructors.
{
//Class *cs = new [1](1);
}
}
}
// # initialize built-in types
//
// http://stackoverflow.com/questions/5113365/do-built-in-types-have-default-constructors
//
// C++ adds new ways to initialize base types.
//
// Those are not however constructors.
//
// They probably just mimic constructor syntax to help blurr the distinction
// between built-in types and classes and make templates and typedefs work better.
{
// Parenthesis initialization
{
int i(1);
assert(i == 1);
}
{
// Most vexing parse.
//int i();
}
{
int i = int();
assert(i == 0);
}
#if __cplusplus >= 201103L
/*
# Brace initialization of scalars
See uniform initialization.
http://stackoverflow.com/questions/14232184/initializing-scalars-with-braces
# Uniform initialization
In c++11 every type can be initialized consistently with `{}`.
Advantages:
- non verbose initialization of multiple structures
- reduces syntax barriers between objects and built-in classes.
Disadvantages:
- ambiguous with initializer list construction!
- implementing the initializer list constructor breaks client code?!
because it has priority over other constructors.
*/
{
// Built-int types
{