[MOVED] Help with constructors

Started by
7 comments, last by 1hod0afop 15 years, 9 months ago
Hi I'd like to call constructor/destructor as I want. I wrote as following:
template <class T> struct _init_
{
	T		Data;

	_init_() {}
	_init_( const T& Object ) : Data(Object) {}
	~_init_() {}

	void* operator new( size_t, T* pObject )
	{
		return pObject;
	}

	void operator delete( void* )
	{
	}

	void operator delete( void*, T* )
	{
	}
};

template <class T> void construct( T* pObject )
{
	new(pObject) _init_<T>;
}

template <class T> void construct( T* pObject, const T& Source )
{
	new(pObject) _init_<T>( Source );
}

template <class T> void destruct( T* pObject )
{
	delete reinterpret_cast< _init_<T>* >( pObject );
}




It's fine but I can't call constructors with parameters. Is there a way to call any constructor of any given class? Thanks [Edited by - 1hod0afop on July 8, 2008 5:26:06 AM]
Advertisement
I'm not sure what you mean. Is it something like this?

template <class T> struct _init_{	T Data;	template <class U>	_init_( const U& Object ) : Data(Object) {}
Quote:Original post by Gage64
I'm not sure what you mean. Is it something like this?

*** Source Snippet Removed ***


Almost. But I'd like to invoke any constructor, regardless how many parameters it has. Something like:
class Foo{public:	Foo( int, int ) {}	Foo( int, int, int ) {}	...};....Foo* pFoo = reinterpret_cast<Foo*>( ::malloc( sizeof (Foo) ) );construct( pFoo, 1, 2 );destruct( pFoo );construct( pFoo, 1, 2, 3 );destruct( pFoo );


Thanks

EDIT: Not necessarily above form
That's an incredibly arcane piece of code. It took me several minutes to figure out what the heck was going on. One thing you need to ask is: why!?

Wouldn't this make more sense:
Foo f1 = Foo(1, 2);

Or this:
Foo* pf2 = new Foo(1, 2, 3);
NextWar: The Quest for Earth available now for Windows Phone 7.
Well that's much more difficult. The source code from this article does something similar using macro hackery.

I also remember SiCrane posting some code that does this, and it was fairly cryptic (to me anyway). Unfortunately I can't find it.

It also had some unavoidable (?) problems (something to do with parameter conversions not working).

If I remember correctly, the conclusion was that it wasn't worth it.

Why do you need to do this, anyway?
Quote:Original post by Sc4Freak
One thing you need to ask is: why!?


Good question. Maybe I'm making things uselessly complicated. I'm implementing a collection class, which stores elements in a list of static-size arrays. The thing is, I don't want to double-initialize when adding element. If I write
template <class T> class arraylist{	...	void add( const T& );	...};...arraylist<Foo> Collection;Collection.add( Foo( 1,2,3 ) );


then (correct if I'm wrong)
1) Foo::Foo(int,int,int) is called to construct a temporary object
2) Foo::Foo(const Foo&) is called (inside arraylist<Foo>::add() function) to copy the object. (which is not necessary)

Some classes have complex copy constructors and I'd like to avoid this. I know I can use class pointer but the main reason I'm implementing this crazy arraylist (different than ArrayList in Java) is to minimize heap operation.

Any ideas? Thanks
First, consider using an STL container like std::vector instead of writing your own.

About avoiding the copy - I think you can use a memory pool for that. boost can help you with this.

I you want to know a bit more about how it works internally, read this. You should probably read this as well.
You can only avoid the copy if you store pointers instead of objects, in which case you'd be best to use a ptr_container from boost, or (if you insist), implement the equivalent of that yourself.
Don't worry about the copy - that may be premature optimisation. The compiler may happen to eliminate the temporary on its own anyway.[smile]

Note that you don't need to implement a copy-constructor when all it does is copy-constructs all the members. Nor should you need to implement an empty default constructor when there are no other constructors. And an empty non-virtual destructor is also unnecessary.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
OK what I tried is being discussed in here. So renaming title [MOVED].

This topic is closed to new replies.

Advertisement