void* GetData()
{
return reinterpret_cast<unsigned char*>(this);
}
Is there a case of automatic type coercion happening in this case ??? How could I convert the object of my class to an unsigned char* ??
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I agree with the other posts: this is almost certainly not what you intend to do.
However, IIRC, you are guaranteed by the C++ standard to be able to convert a pointer of any type (not including function or member pointers) to an
unsigned char *or avoid *and back without entering the realm of undefined behavior. Additionally, you may access any object through an lvalue of typechar *orunsigned char *(see ISO 14882:2003 section 3.10.15). I have made use of this before in order to inspect the internal representation of arbitrary object types. This results, if my reading of the standard is correct, in “implementation-defined behavior” (obviously, the internal representation of types depends upon the implementation).For example,
is a template function that will yield a
std::vector<unsigned_char>which is a copy of the object’s internal representation.However, the fact that you cast to
void *when you return makes me suspect you are doing something perhaps more unsavory and more undefined.