Is it possible to completely disable the default C++ new operator?

前端 未结 4 1240
走了就别回头了
走了就别回头了 2021-02-01 14:03

Because our app has hard performance and memory constraints, our coding standards forbid the use of the default heap — ie, no malloc, no default new

4条回答
  •  失恋的感觉
    2021-02-01 14:21

    If your own "new" operator is not named "new" but differently (e.g. "myNew") you could use a "#define" in a way replacing "new" by rubbish:

    #define new *+-/&
    

    The pre-compiler would now replace a "new":

    x = new mytype;
    

    By the rubbish:

    x = *+-/& mytype;
    

    The advantage compared to a message at linking time is that this will generating a compiler message immediately while compiling the C++ file, not in the end when linking. You also see the line where the "new" is located.

    The disadvantage is that you'll have to "#include" the file containing this "#define" in all C++ files in your project.

提交回复
热议问题