I'm trying to test whether a member of a struct that is declared as a pointer to void was defined before calling a function. However, it seems that whenever I add anything extra to the bare bones program (such as the std::cout at the end), 'testB' always equates to true. I can't change how 'struct a' is declared because it is part of an external library.
So, how do I test that a member of a struct that is declared as a void pointer is in fact defined with the proper type?
Sample structs
struct b { };
struct a {
void *b;
};
Sample code I am trying to test
struct a myA;
struct b myB;
myA.b = &myB;
foo(myA);
Sample test program that is failing
#include <iostream>
int main() {
struct a aa;
struct b bb;
// the following line is commented out so I'm expecting 'testB' to evaluate to false
// aa.b = &bb;
struct b *testB = static_cast<struct b*>(aa.b);
if(testB) {
std::cout << "is defined" << std::endl;
} else {
std::cout << "is not defined" << std::endl;
}
std::cout << "why does adding this line out result in testB equating to true" << std::endl;
return 0;
}