Compiler Error C3828

'object type': placement arguments not allowed while creating instances of managed classes

When creating an object from a managed type, you cannot use the placement form of operator gcnew, ref new or new.

The following sample generates C3828:

// C3828a.cpp
// compile with: /clr
ref struct M {
};

ref struct N {
   static array<char>^ bytes = gcnew array<char>(256);
};

int main() {
   M ^m1 = new (&N::bytes) M();   // C3828
   // The following line resolves the error.
   // M ^m1 = gcnew M();
}

The following sample generates C3828:

// C3828b.cpp
// compile with: /clr:oldSyntax
#using <mscorlib.dll>

__gc struct M {
};

char bytes[256];

int main() {
   M *m1 = new (&bytes) M();   // C3828
   // The following line resolves the error.
   // M *m1 = new M();
}

When using Managed Extensions for C++, the compiler will also generate C3828 if you attempt to create an instance of a managed type in an MFC application. To track memory usage, MFC redefines the new operator when used in a debug version of MFC. Because of this redefinition, errors can be caused by creating instances of managed classes in an MFC application. This typically happens when porting existing MFC code to the common language runtime. For release builds, this error does not occur because MFC does not redefine the new operator.

In the following example, managed code placed in a .cpp file creates an instance of the String class. This causes C3828 when compiled in a debug version of an MFC application.

To avoid this error, temporarily undefine the new operator using the #undef and push_macro directives before creating instances of managed types. After the last line of managed code, restore the previous definition of the new operator using pop_macro.

#pragma push_macro("new")
#undef new

String* s;
s = new String("Hello world!");
#pragma pop_macro("new")