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

hehe, vc++ again

Started by Wavarian Nov 27, 2001 at 8:03 PM 9 replies 450+ views
Original Post
Wavarian
Wavarian
Ok, im getting the hang of c++ now, but i have another query. I used to be able to use dynamic arrays in delphi where i could set a size of elements, assign values to each of them, and then later down the track, resize the array and add some more (keeping the previously set elements as they were). But in c++, i can only see ways of resizing the array discarding all previously entered values for each element.. Am i wrong? can it be done in the way delphi does it? int *dynArray; dynArray = new int[10]; ..set array elements.. dynArray = new int[11]; <-- clearly discards previously set array element values. Thanks again grrr Edited by - wAVaRiaN on November 27, 2001 9:04:43 PM
ATronic
ATronic
There is a custom type called apvector that does that. It basically does this though.

int* blah;
blah = new int[5];
int* temp;
temp = new int[5];
for(int x=0; x<5; x++)
{
temp[x]=blah[x];
}
delete[] blah;
blah = new int[10];
for(int x=0; x<5; x++)
{
blah[x]=temp[x];
}

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
merlin9x9
merlin9x9
apvector? Huh? Judging from the "ap" bit, this sounds like something from the AP curriculum''s code...like their dreaded string class.

No. No. No.

Use vector from the standard template library, which is a class that encapsulates the functionality of a "dynamic" array. And don''t worry about speed--it''s just as dast as "normal" arrays and "dynamic" arrays. Here''s an example of its use:

  
#include <vector>
#include <cstdlib>

// vector and all of the STL components are wrapped in the

// "std" namespace, so use this to bring "std"''s identifiers

// into the current namespace

using namespace std;

int main(int argc, char** argv)
{
// Set up a vector ("dynamic array") to initially hold 10

// elements. We can omit this number and just do vector<int>

// array; instead.

vector<int> array(10);

// fill every element in the array with 42; we find out how

// big the array is with the size() function

for(int i = 0; i < array.size(); ++i)
array[i] = 42;

// ERROR: we can''t access elements that haven''t been

// allocated. In this example, we''d have to have constructed

// the vector with 20 items (instead of 10 like we did), or

// we''d have to resize it before we got here

int x = array[19];

// expand the array to hold 20 items

array.resize(20);

// since the array has been expanded, we can access element

// 19; the expansion preserves existing elements

int y = array[19];

// there''s no need to deallocate anything since a vector

// object automatically deallocates whatever it had

// allocated when it goes out of scope


return EXIT_SUCCESS;
}
ATronic
ATronic
That would be the EXACT syntax of apvector. It also, I''m sure, uses the same meathods. And as a matter of fact, it IS from the ap curriculum. Heh.

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
merlin9x9
merlin9x9
Right, but I''m sure that it isn''t nearly as complete as the STL''s vector. Beyond that, every C++ implementation comes with the STL; it''s effectively part of the language, and as such, there are efficiency guarantees and all of that. AP stuff is evil.
ATronic
ATronic
I won''t argue that! (-:

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
Wavarian
Wavarian
Ack. sorry for bringing this post back from the dead, but what would i do if i have dynamic arrays of custom types and not constant types such as int''s?

Thanks again, and i thought of doing it along the lines of what Atronic posted, but that''s gonna be hell to do with custom typed arrays.
jwace81
jwace81
If you use the stl vector<> class, then it doesn't matter what type of variable you are using, whether it is a custom type, or a built in type.

    
class myClass;
...

vector<myClass> array;


Edit: If you mean that you want a dynamic array of different types of objects, then things become trickier, because you'll have to also store some information about what the type is along with the data.

J.W.

Edited by - jwace81 on December 8, 2001 3:02:03 AM
Oluseyi
Oluseyi
FYI, ATronic''s method - perfectly valid and correct - can be optimized by eliminating the second copy sequence:

int* blah;
blah = new int[5];
.
int* temp;
temp = new int[5];
for(int x=0; x<5; x++)
{
temp[x]=blah[x];
}
delete[] blah;
// simply assign pointers and be done with it.
blah = temp;



[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Fox Mc Cloud
Fox Mc Cloud
Just to add to the info here, if you're dealing with a dynamic array of quite large objects, the copying will cost you a huge lot of time, so you'd better store a dynamic array of pointers to objects, and just new/delete as you create new objects or delete some.

Oh, and if you want to have an array of several object types mixed, the ideal method is polymorphism - derive all objects from a base class and store your objects in an array of pointers to the base class, for instance:

class Object
{
//Relevant attributes/methods that will be common to all types
};
class WallObject:public Object
{
//attributes and methods specific to that type
};
class GroundObject:public Object
{
//attributes and methods specific to that type
};

//then you can use an array of pointers to Objects to store them all
Object **AllObjects;
AllObjects=new Object*[50];
AllObjects[0]=new GroundObject;
for(int i=1;i<50;i++)
AllObjects[ i ]=new WallObject;

//don't forget to delete everything at the end

OK maybe it has not much to do with what was asked lol but I figured I'd still post it in case it interests someone I use polymorphism in all my projects -> see the source of my demo (Julien Barnoin) for a lot of examples... I know it's not optimised and I should have tested it under 98, but I only had the 4 last days to code the whole thing

//Edit : grrr [ i ] makes italic

--
New feature - Windows users, press ALT-F4 to launch the IQ test.

Edited by - Fox Mc Cloud on December 8, 2001 3:54:20 AM
--New feature - Windows users, press ALT-F4 to launch the IQ test.
Wavarian
Wavarian
Thanks guys - all comments helped alot!

now time to try it out in my delphi engine conversion all together..

Topic Locked

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

Sign in to reply to this topic.