Explicitly calling a constructor

Started by
2 comments, last by Z01 18 years, 11 months ago
How can I explicitly call the constructor for a class?

template <class T>
class Allocator3D
{
....
	// Returns NULL on failure
	T *Alloc() 
	{  
		if (!FreeList)
			if (!CreateNewChunk(SizeIncrement))
				return NULL;

		T *ret = FreeList;
		FreeList = Next(FreeList);
		ret->T::T();  // compiler gives error about class not having a T() function

		return ret;
	}
....
};


Advertisement
You should be using placement new, which will not actually allocate the object, just construct one at the address you give.

Edit: One restriction I didn't know of is that the memory you construct it in has to be allocated on the heap.
Quote:Original post by MrEvil
You should be using placement new, which will not actually allocate the object, just construct one at the address you give.


Ohhhh, I didn't know you could do that...[smile]
That sounds what I was looking for, thanks a bunch. Btw, the memory is allocated on the heap for a pooled memory manager.

This topic is closed to new replies.

Advertisement