Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Fragmo

how to call constructor explicitly

This topic is 5652 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an array class that uses malloc/realloc to allocate its memory and I need to know how to call a class constructor explicitly so I can initialize each item as its needed. Is this even possible? I have some code that initializes the items differently but the compiler spews about 30 warnings every time I compile because there''s no matching delete operator. If i can''t call the constructor explicitly how can I get rid of these warnings? c:\dev\common\arrayx.h(171) : warning C4291: ''void *__cdecl CArrayX::Wrapper::operator new(unsigned int,class CStr *)'' : no matching operator delete found; memory will not be freed if initialization throws an exception c:\dev\common\arrayx.h(160) : see declaration of ''new'' c:\dev\common\arrayx.h(170) : while compiling class-template member function ''void __thiscall CArrayX::SetAtIndex(long,class CStr &)'' here''s some of the code in question: class CArray { ... long m_lSize; T* m_pItems; long Add(T& item) { CheckRealloc(1); if (m_lSize < m_lAlloc) { SetAtIndex(m_lSize, item); ++m_lSize; return m_lSize - 1; } return -1; } class Wrapper { public: Wrapper(T& _t) : t(_t) { } template void *operator new(size_t, _Ty* p) { return p; } void operator delete(void*) { } T t; }; void SetAtIndex(long index, T& t) { new(&m_pItems[index]) Wrapper(t); }

Share this post


Link to post
Share on other sites
Advertisement
Sounds like you want placement new, which constructs in a given memory location that already exists. CHeck out :c++ faq lite Or Modern C++ Design, by ALexandrescu.

Share this post


Link to post
Share on other sites
cool, thanks for the link, that helped me fix it

all i needed to do to fix the warnings was add another param to the overloaded delete operator

void operator delete(void*, void* p)
{
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!