Dear experts,
in a C++/CLI project I have a native Logging class with a virtual variadic member function 'Write'. Furthermore, I have a global __LOG variable that holds an instance of that class.
If I call '__LOG.Write(1,"%f",8.34)' from main, it displays the floating point value correctly.
However, if I call '__LOG.Write(1,"%f",8.34)' from a member function of a managed class, the function displays 0.00000. To get the expected output, I need to change the 'Write' function to a non-virtual function.
Can somebody explain this behavior?
#include <cstdarg>
#include <iostream>
#pragma unmanaged
class NativeLog
{
public:
virtual void Write(int aLogType, const char* aFormat, ...) const
{
char str[128];
std::va_list args;
va_start(args, aFormat);
vsprintf_s(str, 128, aFormat, args);
va_end(args);
std::cout << str << std::endl;
}
};
NativeLog __LOG;
#pragma managed
ref class ManagedCaller
{
public:
void Call() { __LOG.Write(1, "%f", 8.34); }
};
int main()
{
__LOG.Write(1, "%f", 8.34);
(gcnew ManagedCaller())->Call();
return 0;
}
What I have tried:
The issue came up in a larger project, and I have isolated it to the code above. I don't need help with the design. I just want to understand the behaviour.