I am studying AssertErrorProc on C++ Builder XE4. I found the delphi code as follows.
procedure AssertErrorHandler(
const iMsg, iFilename: String;
const iLineNo: Integer;
const iAddress: Pointer);
var
Err: String;
begin
Err :=
Format(
'%s (%s line %d @ %x)',
[iMsg, iFilename, iLineNo, Integer(iAddress)]);
ShowMessage(Err);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
AssertErrorProc = AssertErrorHandler;
Assert(false);
end;
I treid to convert the above into C++ code as follows.
void __fastcall TForm1::AssertErrorHandler(const String iMsg,
const String iFilename, const int iLineNo,
const void* iAddress)
{
String Err;
Err = Format(L"%s (%s line %d @ %x)",
[iMsg, iFilename, iLineNo, Integer(iAddress)]); // E2188
ShowMessage(Err);
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
AssertErrorProc = AssertErrorHandler; // E2235, E2268
Assert(false);
}
I received two errors in compiling the code.
at Format() statement (E2188)
at assignment of AssertErrorHandler (E2235, E2268)
I appreciate any information I should modify the code.
Above approach is usable only in Delphi, In C++ you should define custom assert as macro:
#ifdef _DEBUG
#undef assert
#define assert(condition) if(!condition) assertHandler(__FILE__, __LINE__, __FUNCTION__, #condition);
#endif
void assertHandler(const char *fileName, int line, const char *function,
const char *condition)
{
char message[255];
wsprintfA(message, "Assertion failed at %s line %d inside %s condition: %s",
fileName, line, function, condition);
ShowMessage(message);
abort();
}
Usage:
assert(myVar > 0);