Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Can my C function access data in an object of a C++ class?

class Data function object
0
Posted

Can my C function access data in an object of a C++ class?

0

Sometimes. (First read the previous FAQ on passing C++ objects to/from C functions.) You can safely access a C++ object’s data from a C function if the C++ class: • has no virtual functions (including inherited virtual fns) • has all its data in the same access-level section (private/protected/public) • has no fully-contained subobjects with virtual fns If the C++ class has any base classes at all (or if any fully contained subobjects have base classes), accessing the data will TECHNICALLY be non-portable, since class layout under inheritance isn’t imposed by the language. However in practice, all C++ compilers do it the same way: the base class object appears first (in left-to-right order in the event of multiple inheritance), and subobjects follow. Furthermore, if the class (or any base class) contains any virtual functions, you can often (but less than always) assume a “void*” appears in the object either at the location of the first virtual function or as the first word in the obje

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123