This class was defined in my project:
class B : public A
{
public:
A& Get_a(int type);
...
protected:
#ifdef COMPILE_FLAG
int line_num;
const char* file_name;
#endif
...
private:
int int_value;
bool bool-val;
};
The function Get_a
is implemented like this (I put the relevant parts only):
A& B::Get_a(int type)
{
B* returned_a = B->Get_val(type);
return *(A*)(returned_a) ;
}
This is the code where I use this class:
{
...
B b_val;
A* a_val = &b_val->Get_a(5);
...
}
My code is compiled to a different DLL than the DLL where the classes A and B are compiled to.
My DLL doesn't compile with the flag COMPILE_FLAG, but the DLL of A, B does compile with this flag.
I didn't get any compile error even though my code is trying to convert from different B classes.
During my code execution, a_val
had garbage field values; actually shifted values.
I would like some explanations on why the C++ compiler didn't warn me from this bug, and also a coding tip of how to improve these #ifdef
definitions (I was informed that these fields are used for logging).
Edit: if i would write 2 classes with the same name, each class in a different dll. Then, if i would create a reference between them, i would get a compile error. The preprocesor runs before the compiler, so the compiler can check this redefinition of a class.