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 );
}
🔒 Locked
[MOVED] Help with constructors
Advertisement
Original Post
Hi I'd like to call constructor/destructor as I want. I wrote as following: 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]
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
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?
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
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
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.
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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement