Skip to main content
GameDev.net gamedev.net
🔒 Locked

Sticking Predefined Values into Member Array

Started by Boder Feb 25, 2005 at 11:28 PM 4 replies 650+ views
Original Post
Boder
Boder
I have struggled over this for many a time, and I need to know the way to do this most elegantly.

class test
{
  int array[20];
}

// And I need to get these values into the array as part of the constructor
// {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10}


nts
nts
AFAIK you can't specify explicit initializers for arrays in the constructors (with MIL). You can just write a loop to fill the array in the constructor (if the values are incrementing like the example) or if the array is going to be static you can use a static initializer for it which will allow you to specify what to fill the array with.

HTH
Treiber
Treiber
Wouldn't two for loops be the best way?
I am sorry but I don't know if this was exactly your question.
Gimme fuel, gimme fire, gimme that which I desire.Metallica / Fuel
Boder
Boder
Okay, here's what I have.

test::test(){  array[0]  = 1;  array[1]  = 2;  array[2]  = 3;  array[3]  = 4;  array[4]  = 5;  array[5]  = 6;  array[6]  = 7;  array[7]  = 8;  array[8]  = 9;  array[9]  = 10;  array[10] = 1;  array[11] = 2;  array[12] = 3;  array[13] = 4;  array[14] = 5;  array[15] = 6;  array[16] = 7;  array[17] = 8;  array[18] = 9;  array[19] = 10;}


Or would this be faster.

test::test(){  int temp[] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10};  assert( sizeof(array) == sizeof(temp) );  std::memcpy(array, temp, sizeof(temp));}
nts
nts
what about just

for (i = 0; i < 20; i++){    array = (i % 10) + 1;}


EDIT: made a mistake, i think
Jingo
Jingo
In this case you can use

struct test{  int array[20];};test instance = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10};

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.