Including the class as a member in the structure

I need to add a class object as a member inside a c-structure.

Is there any ban on this.

Regards, ISight

+7
source share
4 answers

You have a member of the C ++ class in C, but you need to consider it as void* at point C, since C can handle it in order.

This method is called Opaque Pointer .

+1
source

I assume that you are talking about C ++, since there is no concept of “class” in C, although you can certainly have a struct as a member of another struct .

In addition to one minor detail, class and struct identical, and both are often called "class types". Everything that you can do with class (for example, with a member of a class type) can also be done with struct .

If you're interested, the only difference is the default accessibility for members and base classes; public for struct and private for class .

+4
source

No no. Check out this example:

 #include<iostream> class Foo { public: Foo() { this->i = 1; } int i; }; struct Bar { Foo foo; }; int main() { struct Bar bar; std::cout << bar.foo.i << std::endl; return 0; } 
0
source

While the structure is simply used in C ++ code, there are no problems. However, if the structure is passed to C code, bad things can happen (the destructor is not called when the struct is freed / deleted).

If you don't see anything like extern "C" in a declaring file, you are likely to be safe.

0
source

All Articles