Skip to main content
gamedev.net
🔒 Locked

[MOVED] Help with constructors

Started by 1hod0afop 7 replies 1.1k views
Original Post
1hod0afop
1hod0afop
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]
1hod0afop
1hod0afop
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
Sc4Freak
Sc4Freak
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);
Gage64
Gage64
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?
1hod0afop
1hod0afop
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::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
Gage64
Gage64
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.
iMalc
iMalc
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.
1hod0afop
1hod0afop
OK what I tried is being discussed in here. So renaming title [MOVED].

Topic Locked

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

Sign in to reply to this topic.