Skip to main content
GameDev.net gamedev.net
🔒 Locked

Calling a constructor

Started by Newfound Ajarn Mar 8, 2004 at 6:49 PM 9 replies 1.2k views
Original Post
Newfound Ajarn
Newfound Ajarn
Well, I've looked all over Google and Gamedev's search page and I still havent found a way to call a class constructor. I'm making amemory manager and I have it call new for an element, and when it's done it calls the destructor for that class. So, then I have the memory manager give out that memory the next time it needs a new class object. All is fine, but I cant call the class constructor. I'll give some examples of what I'm talking about.
template< typename T >
class MmoryManager
{
    T* New()
    {
        // Check to see if there is some "free" memory

        if( bFreeMemory )
        {
            // This is what I can't do

            //    pData is a pointer to the "free" deconstructed object

            pData->T(); // or pData->T::T();

        }
        else
        {
            return new T; // also keep track of this creation, but thats internal stuff

        }
    }

    void Delete( T *ppDel )
    {
        ppDel->~T(); // or ppDel->T::~T();

        // Some internal tracking of the memory for later use

    }
}
I have tried making a local copy of T and memcpy the data into the "free" object, but something seems to be going wrong with my code, and I can't figure out what that is. So, the main question: Is there a way to call the constructor for a class, or a alt for what I'm trying to do? [edited by - newfound ajarn on March 8, 2004 7:53:19 PM]
VLAjarn: Cause it (linux) NEVER crashesMrPr0Grmer: lol ur wrongMrPr0Grmer: RedHat crashesVLAjarn: I'm saying good builds of linux
Oluseyi
Oluseyi
pData = new T();
..
delete pData;
Doc
Doc
If you already have some memory pool that you wish the data to be constructed in there is a way to tell the new operator exactly where you want to put the object. Something like new (pData) T(); (Someone will correct me if I''m wrong.) I believe its then your resposibility to make sure the memory is indeed already allocated. I''m not too sure - someone help elaborate?
Newfound Ajarn
Newfound Ajarn
That would work well (because that's the normal way of doing it), but I'm trying to make it so I only have to call new and delete once and reuse the memory I have by calling the constructor and destructor manually.

Doc: I would love to do it that way, but I don't know how to do that either. If someone could tell me how to do that it would solve all my problems.

[edited by - newfound ajarn on March 8, 2004 8:00:03 PM]
VLAjarn: Cause it (linux) NEVER crashesMrPr0Grmer: lol ur wrongMrPr0Grmer: RedHat crashesVLAjarn: I'm saying good builds of linux
Oluseyi
Oluseyi
quote:
Original post by Newfound Ajarn
That would work well (because that''s the normal way of doing it), but I''m trying to make it so I only have to call new and delete once and reuse the memory I have by calling the constructor and destructor manually.
Google "placement new".
SiCrane
SiCrane
It''s called "placement new". Example:

void * void_ptr = malloc(sizeof(T));
T * t_ptr = new (ptr) T();

To delete:

t_ptr->~T();
free(void_ptr);
Newfound Ajarn
Newfound Ajarn
Thank you all! That''s what I was looking for.
VLAjarn: Cause it (linux) NEVER crashesMrPr0Grmer: lol ur wrongMrPr0Grmer: RedHat crashesVLAjarn: I'm saying good builds of linux
iMalc
iMalc
Oh man, why didn''t someone tell me about this when I was writing virtually the same thing some time ago!
DrPizza
DrPizza
One cannot have a class with no c-tor or d-tor. The c-tor or d-tor might be implicit, and they might be inaccessible, but they will always be there.

Unless you mean a class where the c-tor or d-tor are declared but not actually defined. No, that wouldn''t work, it should generate a linker error.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
Newfound Ajarn
Newfound Ajarn
My thing works with any type (int, float, cClassName), the typename T solves it (if there is not destructor, the compiler takes care of it). If you want to see my finally working version just ask here and I''ll provide a link (later today, because of stupid school).
VLAjarn: Cause it (linux) NEVER crashesMrPr0Grmer: lol ur wrongMrPr0Grmer: RedHat crashesVLAjarn: I'm saying good builds of linux

Topic Locked

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

Sign in to reply to this topic.