// example for initialization // #include #include #include #include #include using namespace std; struct bingocall { char letter; int number; }; class bingorange { public: int lowest, highest; bingorange(int min, int max) { lowest=min; highest=max; } }; void writecall(const bingocall &bc) { cout << bc.letter << ',' << bc.number << endl; } bingorange letterrange3(char l); bingorange letterrange2(char l); bingorange letterrange(char l); int main(int argc, char **argv) { char firstletter = 'B'; // "copy" syntax (*NOT* an assignment op call) char anotherletter('I'); // "direct" syntax bingorange Irange = bingorange(16,30); // fn call syntax bingocall call1 = {'G',56}; // brace initialization char cardcolumns[] = {'B','I','N','G','O'}; // brace initialization again // below works in C++11 only vector columnnames {'B','I','N','G','O'}; // below, error if number of el > 5 array colnames = {'B','I','N','G','O'}; // NOTE: = {} and {} are the same // except that = {...} does not allow the use of explicit constructors vector calls { {'B',5}, {'O',71}, {'I',22} }; for(auto &&x : calls) writecall(x); cout << endl; // narrowing not allowed w/ init lists: bingocall call2; call2.letter = 'N'; call2.number = 33.33; // allowed //bingocall call3 {'N', 33.33}; // compile error -- narrowing (NEW!) // heap allocation also okay: (no "=" allowed here) bingocall *callptr = new bingocall[3] { {'B',5}, {'O',71}, {'I',22} }; // brace init syntax can be used anywhere now bingorange Brange = bingorange{1,15}; // calls constructor writecall({'B',1}); // calls constructor and passes into function // see below, after main too bingorange Grange = letterrange('G'); cout << "G: " << Grange.lowest << ' ' << Grange.highest << endl; bingorange Xrange = letterrange('X'); cout << "X: " << Xrange.lowest << ' ' << Xrange.highest << endl; Grange = letterrange2('G'); cout << "G: " << Grange.lowest << ' ' << Grange.highest << endl; Xrange = letterrange2('X'); cout << "X: " << Xrange.lowest << ' ' << Xrange.highest << endl; Grange = letterrange3('G'); cout << "G: " << Grange.lowest << ' ' << Grange.highest << endl; Xrange = letterrange3('X'); cout << "X: " << Xrange.lowest << ' ' << Xrange.highest << endl; } class brangecalled : public bingorange { set used; public: // note use in the next line (no "=" allowed here) brangecalled(int min, int max) : bingorange{min,max}, used{} { } void call(int v) { if (v>=lowest && v<=highest) used.insert(v); } bool called(int v) const { return used.find(v)!=used.end(); } }; bingorange letterrange(char l) { // there are better ways, but this lets me demonstrate... switch(l) { case 'B': return {1,15}; // note brace initialization of the return case 'I': return {16,30}; case 'N': return {31,45}; case 'G': return {46,60}; case 'O': return {61,75}; default: return {0,0}; } } // could also have been written as int minval(char l) { switch(l) { case 'B': return 1; case 'I': return 16; case 'N': return 31; case 'G': return 46; case 'O': return 61; default: return 0; } } bingorange letterrange2(char l) { // function eval fine inside (just like normal constructor) int low; // yes, below is guarenteed to work -- they are eval'd L->R // I do NOT advocate this code -- it is just showing how the language // works return {low=minval(l),low?low+15:0}; } // or (better) bingorange letterrange3(char l) { static map rangemap = {{'B',{1,15}},{'I',{16,30}},{'N',{31,45}},{'G',{46,60}},{'O',{61,75}}}; auto res = rangemap.find(l); if (res==rangemap.end()) return {0,0}; return res->second; }