Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#Actualfastcall22

Posted 15 July 2012 - 11:59 AM

Okay, so you'd like to resize the array to make room for a new Object*.  In C++, this is done using std::vector instead of raw arrays:
std::vector<Object*> colList;

void Player::addToList( Object* object ) {
    colList.push_back( object );
}

int Player::getColListSize() const {
    return colList.size();
}

Using raw arrays, you need to reallocate enough space to hold N+1 Object*s, and move those Object*s from the old array into the new array, and destroy the old array:
Object* newArr = new Object*[N+1];
for ( int idx = 0; idx < N; ++idx )
    newArr[idx] = colList[idx];
delete[] colList;
colList = newArr;


Most implementations of std::vector do the same as above behind the scenes.

#2fastcall22

Posted 15 July 2012 - 01:51 AM

Okay, so you'd like to resize the array to make room for a new Object*.  In C++, this is done using std::vector instead of raw arrays:
std::vector<Object*> Object;

void Player::addToList( Object* object ) {
    Object.push_back( object );
}

int Player::getColListSize() const {
    return Object.size();
}

Using raw arrays, you need to reallocate enough space to hold N+1 Object*s, and move those Object*s from the old array into the new array, and destroy the old array:
Object* newArr = new Object*[N+1];
for ( int idx = 0; idx < N; ++idx )
    newArr[idx] = colList[idx];
delete[] colList;
colList = newArr;


Most implementations of std::vector do the same as above behind the scenes.

#1fastcall22

Posted 15 July 2012 - 01:49 AM

Okay, so you'd like to resize the array to make room for a new Object*.  In C++, this is done using std::vector instead of raw arrays:
std::vector<Object*> Object;

void Player::addToList( Object* object ) {
    Object.push_back( object );
}

int Player::getColListSize() const {
    return Object.size();
}

Using raw arrays, you need to reallocate enough space to hold N+1 Object*s, and move those Object*s from the old array into the new array, and destroy the old array:
Object* newArr = new Object*[N+1];
for ( int idx = 0; idx < N; ++idx )
    newArr[idx] = Object[idx];
delete[] Object;
Object = newArr;


Most implementations of std::vector do the same as above behind the scenes.

PARTNERS