By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,128 Members | 926 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,128 IT Pros & Developers. It's quick & easy.

class - array - initialize in constructor

P: 9
Hi!!
I want to use an array into a function of an object.I decided to declare the array in the class and to initialize its values with the constructor.I wrote something like this:
Expand|Select|Wrap|Line Numbers
  1. class my_class{
  2.         public:
  3.                  ....... 
  4.                  int my_array[15];
  5.                  ......
  6.        }
  7. my_class::my_class()
  8. {
  9.             my_array{0,1,2,3....,14};
  10.                  }
  11.  
but the compiler found some problems.How i can do this?how i can call the array when i am in a function of the class? Any help and ideas are acceptable

Maradona
=======
Dec 22 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
You can't iitialise an array like this, you have to do it something like

Expand|Select|Wrap|Line Numbers
  1. my_class::my_class()
  2. {
  3.     my_array[0] = 0;
  4.     my_array[1] = 1;
  5.     my_array[2] = 2;
  6.     my_array[3] = 3;
  7.     ...
  8.     my_array[14] = 14;
  9. }
  10.  
Of course in this case you could always use a loop to reduce the number of code lines.
Dec 24 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.