Public operator new, private operator delete: getting C2248 "can not access private member" when using new

Viewed 3137

A class has overloaded operators new and delete. new is public, delete is private.

When constructing an instance of this class, I get the following error:

pFoo = new Foo(bar)

example.cpp(1): error C2248: 'Foo:operator delete': cannot access private member declared in class 'Foo'

But there's no call to delete here, so what is going on in the twisted mind of the compiler? :)

  1. What is the reason for the error?
  2. Is it possible to resolve the problem without resorting to a member CreateInstance function?
5 Answers

Calling operator new on class will use also delete class if class constructor throws an exception.

If your library does not use exceptions, you can disable exceptions from compiler "-fno-exceptions", then error won't appear anymore. (In Visual studio resides under "C/C++", "Code Generation", "Enable C++ Exceptions" > "No (-fno-exceptions)")

Related