Sign in to follow this  

Init Vector // Manually calling construct oe

This topic is 4305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've got some problems with the vector class. I programmed a class myself. It includes a vector. Now the problem: I dynamically create this class (allocate memory (malloc)). The problem is, when i create the class, it doesnt call any constructors. I made an Init method which i call, but I got the problem that theres no way to init the vector... any ideas?

Share this post


Link to post
Share on other sites
malloc allocates a block of memory of the size you requested on the heap.

new "calls malloc", then does the C++ constructor magic on the resulting block of memory.

Share this post


Link to post
Share on other sites
Thanks for the replies.
One short question:
i have a vector: animations
I never used "new", always did it with malloc
so how do i use "new" it with a vector?
animations = new vector(???);
I have no idea what the params are and with the tooltips of vb2005 i can't get it to work neiteher ^_^

Share this post


Link to post
Share on other sites
Quote:
Original post by kain0015
Thanks for the replies.
One short question:
i have a vector: animations
I never used "new", always did it with malloc
so how do i use "new" it with a vector?
animations = new vector(???);
I have no idea what the params are and with the tooltips of vb2005 i can't get it to work neiteher ^_^
Are you talking about a std::vector<>, an array, or your own vector class? You might post some examples from your code.

The syntax for creating and deleting a simple dynamically allocated array in C++ is:
Object* objects = new Object[numberOfObjects];
/* ... */
delete [] object;

Share this post


Link to post
Share on other sites
Quote:
Original post by kain0015
Thanks for the replies.
One short question:
i have a vector: animations
I never used "new", always did it with malloc
so how do i use "new" it with a vector?
animations = new vector(???);
I have no idea what the params are and with the tooltips of vb2005 i can't get it to work neiteher ^_^

If you are refering to std::vector [jyk covered the array situation], then you probably don't want to call new at all. The vector will construct itself automatically.

What you want is to use new for the enclosing class...example:

class Sprite
{
std::vector<int> Animations;
}

int main()
{
Sprite* good = new Sprite();
//now, good.Animations has been created and everything is OK.

Sprite* bad = (Sprite*)malloc(sizeof(Sprite)); //I'm guessing as to malloc's signature
//now, bad.Animations has *not* been created, so nothing is OK.
}


If you need help using the vector once it has been created, feel free to ask.

CM

Share this post


Link to post
Share on other sites
Thanks for the replies!
Got it to work!
But when I create an object like this:
Object object = new Object[5];
Is it possible to resize it? (like realloc?)

Share this post


Link to post
Share on other sites
Quote:
Original post by kain0015
Thanks for the replies!
Got it to work!
But when I create an object like this:
Object object = new Object[5];
Is it possible to resize it? (like realloc?)

Not easily. That's why you use vectors.


std::vector<Object> vec;
vec.push_back(Object(1));
vec.push_back(Object(2));
//now, vec has two objects [Object(1) Object(2)]

vec = std::vector<Object>(3, Object(5));
//now, vec has three objects, all copies of Object(5)
vec.push_back(Object(4));
//now, vec has four objects [Object(5) Object(5) Object(5) Object(4)]


CM

Share this post


Link to post
Share on other sites
Quote:
Original post by kain0015
Is it possible to resize it? (like realloc?)
You can, but you have to do it manually. Generally it's easier to use std::vector<> or another stl container, which will take care of those sorts of details for you, more or less.

Share this post


Link to post
Share on other sites
Sorry if this is going off-topic, but sometimes I feel sheepish about using std::vector. I know it's likely an incredible implementation, but is something like realloc() faster? (in terms of adding a P.O.D. to the end of the vector, say)


discman1028

Share this post


Link to post
Share on other sites

This topic is 4305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this