Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

object alloc with non-default constructor

Name: Anonymous 2008-11-21 23:01

I can't figure out how to instantiate a dynamic array of objects with a call to a specific constructor - it should do something like this:

Image* images = new Image(Image::RGB,w, h, ...)[num];  //doesn't work

instead of

Image* images = new Image[num];  //this calls goddamn default constructor

Name: Anonymous 2008-11-21 23:06

>>1
Forget it, it's NP complete.

Seriously, though, you can't do it. That's why, if you want to have an array T, T must have a default constructor, because that's the constructor that will get called.

You could create an array of pointers instead.


Images** images = new Image*[num]; // I think that's the syntax.
for(int i = 0; i < num; ++i)
    images[i] = new Image(Image::RGB, w, h, ...);

Name: Anonymous 2008-11-21 23:10

Impossible? Pretty weird.

Anyway thanks for that snippet, I was also trying to figure out that pointer array syntax.

Name: Anonymous 2008-11-21 23:19

Use a real OO language.

Or at the very least stop assuming everyone uses only the language you do when you make a post.

Name: Anonymous 2008-11-21 23:46

If you really want them to be contiguous, I suggest mallocing an appropriately-aligned array of the correct size and initializing them manually using placement new. It's not pretty, though.

How malloc handles alignment depends on your system, I think. It may not be appropriately aligned.

Image* images = reinterpret_cast<Image*>(malloc(sizeof(Image) * num);
for(int i = 0; i < num; ++i)
    new (images[i]) Image(Image::RGB, w, h, ...);


Of course, you can't delete[] this array, so you'll have to free() it and destruct the images manually. You could also wrap this in a RAII class with little effort.

PS. Sepplesux

Name: Anonymous 2008-11-22 0:04

I appreciate the help.


>>4

Obviously this isn't for theoretical edification. We can't use java for everything.

Name: Anonymous 2008-11-22 0:33

>>6
Java is theoretically edifying? Haven't you heard of Haskell?

Name: Anonymous 2008-11-22 0:56

>>6
Obviously this isn't for theoretical edification. We can't use java for everything.

And as this thread shows, we can't use Sepples for anything. Seriously, what were you thinking?

Name: Anonymous 2008-11-22 17:12

>>1
The magic words are "placement new" HTH HAND

Name: Anonymous 2008-11-22 23:22

std::cout << "I am a proud Sepplesfag." << std::endl;

Name: Anonymous 2008-11-23 0:11

ɹǝɯɯɐɹƃoɹd ʇɹǝdxǝ uɐ ɯɐ ı

Name: Anonymous 2009-03-06 7:43

Appropriately overengineer projects with   a lot of   people have learned   all the programming   environment sucks ass   Fortunately the Mindstorms   platform is open   source but the   head of a   distributed password cracking   program called CrackAtHome.

Name: Anonymous 2009-03-06 9:32

was looking for advice   IN LOVE SALES   AND GOOD SEX   WHEN I FART   IN THE WINTER   break I have   over 10 years   old holding many   C books I   suggest Game Coding   Complete 2nd Edition   System Tray and   Task Bar with   bookmarks of differents   sections of 4chan   is you do   the perl trick   of skipping whitespace   it gets shorter.

Name: Anonymous 2009-03-06 13:02


Weird as the car exhaust pipe fetish.

Name: Anonymous 2010-12-25 0:42


Don't change these.
Name: Email:
Entire Thread Thread List