Hmm, why does placement new call my overloaded Foo new if it exists, but doesn't otherwise call the overloaded global new.
#include <new>#include <stdio.h>void* ::operator new(size_t size) { printf("\nglobal new"); return malloc(size);}// Dummy class, ignore.class Bar { int m_y;};class Foo { public: Foo() { printf("\nctor"); } ~Foo() { printf("\ndtor"); } //void* operator new(size_t size, void* place) { printf("\nnew called"); return place; } // <----- (#1) I DON'T ACTUALLY WANT THIS TO BE CALLED. //void* operator new(size_t size, Bar *bar) { return malloc(size); } //void* operator new(size_t size) { return malloc(size); } void operator delete(void* data, Bar *bar) { free(data); } private: int m_x;};int main() { freopen("stdout.txt", "wb", stdout); printf("test"); void* raw = malloc(sizeof(Foo)); // Allocate the raw memory to be used in the placement new. Foo* p = new(raw) Foo(); // Attempt the placement new. <----- THIS IS WHERE THE BUG IS. p->~Foo(); // Placement delete. free(raw); // Remove the memory. return 1;}
If I uncomment #1, it calls my overloaded new.
Outputs:
#1 uncommented. Unfortunately, "new" is called.
#1 commented. No "new" called.
Maybe I should rephrase the question:
How can I call a constructor (without actually calling an "operator new" function) after I have overloaded new.
Quote:From AP
If you want to reinitialize an object, why not just make an init-function and have the constructor call that? You can then call this init again when you want.
It's just me playing with allocation syntax, so I'd have to write an init-function for every class.