CSimpleArray ?
-
Excuse me. Today,I study the WTL Library's CSimpleArray class source code,I find the following code:
template<class T> class CSimpleArray { T* m_aT; ... class Wrapper { public: Wrapper(T& _t) : t(_t) { } template void *operator new(size_t, _Ty* p) { return p; } T t; }; void SetAtIndex(int nIndex, T& t) { new(&m_aT[nIndex]) Wrapper(t); } ... }
I can not understand the syntax about "new(&m_aT[nIndex]) Wrapper(t)":confused:,anyone can explain it? thanks first. SIMPLE IS BEAUTY
-
Excuse me. Today,I study the WTL Library's CSimpleArray class source code,I find the following code:
template<class T> class CSimpleArray { T* m_aT; ... class Wrapper { public: Wrapper(T& _t) : t(_t) { } template void *operator new(size_t, _Ty* p) { return p; } T t; }; void SetAtIndex(int nIndex, T& t) { new(&m_aT[nIndex]) Wrapper(t); } ... }
I can not understand the syntax about "new(&m_aT[nIndex]) Wrapper(t)":confused:,anyone can explain it? thanks first. SIMPLE IS BEAUTY
ZBUILDER wrote: I can not understand the syntax about ZBUILDER wrote: new(&m_aT[nIndex]) Wrapper(t); returns the pointer and that new allocates using that pointer * * * Code the dreams * * *
-
ZBUILDER wrote: I can not understand the syntax about ZBUILDER wrote: new(&m_aT[nIndex]) Wrapper(t); returns the pointer and that new allocates using that pointer * * * Code the dreams * * *
-
ZBUILDER wrote: I can not understand the syntax about ZBUILDER wrote: new(&m_aT[nIndex]) Wrapper(t); returns the pointer and that new allocates using that pointer * * * Code the dreams * * *
It's called placement new. You're executing the Wrapper-constructor on an already allocated memory block. -- Only in a world this shitty could you even try to say these were innocent people and keep a straight face.
-
Excuse me. Today,I study the WTL Library's CSimpleArray class source code,I find the following code:
template<class T> class CSimpleArray { T* m_aT; ... class Wrapper { public: Wrapper(T& _t) : t(_t) { } template void *operator new(size_t, _Ty* p) { return p; } T t; }; void SetAtIndex(int nIndex, T& t) { new(&m_aT[nIndex]) Wrapper(t); } ... }
I can not understand the syntax about "new(&m_aT[nIndex]) Wrapper(t)":confused:,anyone can explain it? thanks first. SIMPLE IS BEAUTY
ZBUILDER wrote: new(&m_aT[nIndex]) Wrapper(t) It's called placement new. It executed the constructor Wrapper(t) on a preallocated memory block &m_aT[nIndex]. So basically, you're telling new not to allocate new memory for the object but use existing memory. -- Only in a world this shitty could you even try to say these were innocent people and keep a straight face.