简体   繁体   中英

Casting class into a struct

I have this :

class masterStruct {

public:

int counter; 
bool synchronized;
std::string slaveIDs[3];

void test() { counter = 0; }

}

It's defined in my class like this:

class MasterInterface {

public:
  struct masterStruct masStruct;

} 

And I use masStruct in C-style callback functions.

My question is : What happens to the test() function? I know struct and class are essentially the same with class default member variable set to private. Why does c++ allow casting class to struct in this case, where the test() function seems to be no longer valid?

The only difference between class and struct is that, in the definition of the type, the default access level is private or public respectively. Otherwise, and in other contexts (such as this one), they are identical.

class masterStruct and struct masterStruct are two names for the same type. There's no casting because there's only one type. C++ allows the keywords to be used interchangeably because the default access level is no more than a minor implementation detail within the definition.

C++ class types (declared with class or struct ) can be accessed from C functions as long as certain C++ features can't be used. virtual functions, mixed access protection for members, etc. can affect the layout of the members in memory, so the C compiler won't be able to find them. "POD" (plain old data) classes are a slight superset of what C will understand, because certain kinds of single inheritance are also permitted.

Nonstatic member functions don't add anything to a class in memory, so they don't affect its layout. They're just ordinary functions with special rules for passing an additional argument which implements this . C doesn't need to know about them, so you can safely #ifndef __cplusplus them out.

However, it doesn't sound like you're using masterStruct from C code at all, but you're only passing a void* through a C library and back to yourself. In this case, POD-ness doesn't make a difference either. The void* value is just a scalar value, and it will be the same when you pass it and when you get it back. It could point to anything, or to nothing; it doesn't matter.

Why does c++ allow casting class to struct in this case, where the test() function
seems to be no longer valid? 

In C++, struct can also have functions. So you can cast a class object to an object of the according struct , and access the member functions via that struct object. Note that the access specifiers( public , private , or protected ) in the original class definition determines how the member functions are accessed. Casting to struct doesn't redefine those functions as public , which you may wonder.

I guess your real question is why a C++ struct (or class as in your original question) can be passed to a C API where there is no member function concept in the language?

This is doable due to Plain Old Data(POD) types support in C++. POD objects in C++ are interchangable with C. That's why you can pass it to C API. In short, A POD type is a struct or class without user-defined constructors, destructors and virtual member functions, which is exactly your case. For a detailed discussion, refer to this thread .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM