c++ - How to use base member initialization on an array? -


making blackjack game , need know how initialize array using base member initialization (i.e. intializing within constructor using unary scope resolution operator).

//constructor card::card() :mrank(static_cast<rank>(ace)), msuit(static_cast<suit>(spades)), mranktext(), msuittext() { } 

i want initialize ragged arrays mranktext , msuittext

const char * ranktext[number_of_ranks] = {"ace",                                            "deuce",                                            "trey",                                            "four",                                            "five",                                            "six",                                            "seven",                                            "eight",                                            "nine",                                            "ten",                                            "jack",                                            "queen",                                            "king"};  const char * suittext[number_of_suits] = {"spades",                                            "clubs",                                            "diamonds",                                            "hearts"}; 

base member initialization (i.e. intializing within constructor using unary scope resolution operator).

it's called ctor-initializer or mem-initializer-list (or "constructor initializer list") , there's no such thing "the unary scope resolution operator" that's called colon!

prior c++11 can't this, can leave array uninitialized or initialize members 0 (which code doing now.)

in c++11 can use uniform initialization braces:

//constructor card::card() : /*...*/, mranktext{ a, b, c }, msuittext{ d, e, f, g } { } 

that requires explicitly specifying each element, automate need use delegating constructor , variadic templates. following uses std::index_sequence type in c++14, can today integer_seq.h implementation:

//constructor card::card() : card(std::make_index_sequence<number_of_ranks>(),        std::make_index_sequence<number_of_suits>()) { }  template<size_t... r, size_t... s> card::card(std::index_sequence<r...>, std::index_sequence<s...>) : /*...*/, mranktext{ ranktext[r]... }, msuittext{ suittext[i]... } { } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -