array of a class without default constructors

Started by
20 comments, last by DigitalDelusion 19 years ago
I have one other question...

The following code compiles, but only initializes the first object. Is there any way to initialize the entire array? That's pretty much the entire point of this thread.

#include <new>#include <iostream>using namespace std;class A{public:	A(const int src_x)	{		x = src_x;	}	~A()	{	}	int x;};int main(void){	void* buf = ::operator new(sizeof(A) * 50);	A* a = new(buf) A(50);	a->~A();                                    	::operator delete(buf);	return 0;}
Advertisement
Well you could simply borrow my code snippet and send your pointer and size instead of mine and it would work :p
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
I guess one could use the = operator...

[source lang = "cpp"]#include <new>#include <iostream>using namespace std;class A{public:	A(const int src_x)	{		x = src_x;	}	~A()	{	} A& A::operator=(const A& rhs) {	if(&rhs == this)		return *this;	x = rhs.x; 	return *this; } 		int x;};int main(void){	void* buf = ::operator new(sizeof(A) * 50);	A* a = new(buf) A(999);	for(size_t i = 0; i < 50; i++)		a = a[0];	cout << a[49].x;	a->~A();                                    	::operator delete(buf);	return 0;}
Quote:Original post by DigitalDelusion
Well you could simply borrow my code snippet and send your pointer and size instead of mine and it would work :p


I don't get your drift...
Quote:Original post by Anonymous Poster
Quote:Original post by DigitalDelusion
Well you could simply borrow my code snippet and send your pointer and size instead of mine and it would work :p


I don't get your drift...


you could simply use the raw_init and raw_kill functions from eariler in this thread.

also using op= could potentially cause very bad things (tm) since that could have the not so nice sideeffect of invoking the dtor.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Another thing: how does one call the destructor on all of the objects in the array?

a->~A();

only destructs the first one.

for(size_t i = 0; i ~A();

is not legal.
Quote:Original post by DigitalDelusion
Quote:Original post by Anonymous Poster
Quote:Original post by DigitalDelusion
Well you could simply borrow my code snippet and send your pointer and size instead of mine and it would work :p


I don't get your drift...


you could simply use the raw_init and raw_kill functions from eariler in this thread.

also using op= could potentially cause very bad things (tm) since that could have the not so nice sideeffect of invoking the dtor.


I see. Sorry, I was not paying close enough attention. Thanks for the info. :)
Quote:Original post by Anonymous Poster
Another thing: how does one call the destructor on all of the objects in the array?

a->~A();

only destructs the first one.

for(size_t i = 0; i ~A();

is not legal.


well, you could have a look at the raw_kill function eariler in this thread..

or if you're lazy
for(size_t i = 0; i != N; ++i) a.~A();

HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Quote:Original post by DigitalDelusion
Quote:Original post by Anonymous Poster
Another thing: how does one call the destructor on all of the objects in the array?

a->~A();

only destructs the first one.

for(size_t i = 0; i ~A();

is not legal.


well, you could have a look at the raw_kill function eariler in this thread..

or if you're lazy

for(size_t i = 0; i != N; ++i)
a.~A();



My mistake was using -> instead of . during that destruction loop. I should have checked your code first -- thanks :)
Quote:Original post by Anonymous Poster
I see. Sorry, I was not paying close enough attention. Thanks for the info. :)


happens to all of us in the middle of the night I guess ;)

now go get a login instead of hiding behind that silly AP :p
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats

This topic is closed to new replies.

Advertisement