Initializing an array within a c++ constructor -


this question has answer here:

i want have array class member, way know how initialize array in constructor adding each element individually (array[x] = char)

class myclass {   public:     myclass();     ~myclass();     void printletters(); // prints each character in array   private:     int alpha[3]; // allocate memory array };  myclass::myclass() {     // initialize array     alpha[0] = 1;     alpha[1] = 2;     alpha[2] = 3;      printletters(); }  myclass::~myclass() { }  void myclass::printletters() {     (int x = 0; x < 3; x += 1)     {         cout << alpha[x] << endl;     } }  int main() {     myclass abc;      return 0; }   

is there way it? if try this:

myclass::myclass() {     // initialize array     alpha[3] = {1, 2, 3};      printletters(); }  

i following error: expression syntax in turbo c++

i believe in gcc @ least, -std=c++11, can this:

myclass::myclass() : alpha{1, 2, 3} {     printletters(); } 

this array initialization in member initialization list. it's standard c++ feature, compilers (cough vc++ cough) might not have implemented yet.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -