C++ placement new keep giving compiling error

前端 未结 2 1046
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 04:14

Try to use placement new but it kept giving me errors. I remember a while ago, it was working. g++ (ver 4.8.4) on Ubuntu 14.04.

#include 
typed         


        
相关标签:
2条回答
  • 2021-01-21 04:33

    To make your original code work, you need to add

    void* operator new( size_t, strSession * p ) { return p; }
    

    In the old days, before C++ left Bell Labs, C++ had a feature where a constructor could assign to 'this'. The operator new placement syntax was considered an improvement.

    0 讨论(0)
  • 2021-01-21 04:48

    To use placement new, you need to have:

    #include <new>
    

    Also, you could just as easily have used:

    int main(int argc, char *argv[]) {
        char buf[20];
        strSession *q = new (buf) strSession;
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题