say a define a struct in header file of c++, foo.h
typedef struct {
...
}foo;
in .c file I include the foo.h, then
foo* fooPtr;
will it work?
say a define a struct in header file of c++, foo.h
typedef struct {
...
}foo;
in .c file I include the foo.h, then
foo* fooPtr;
will it work?
It depends a lot on what is actually in the struct foo
. If the struct is simply data and has no behavior itself or in any of it's member fields then it should work just fine in C or C++. If it has any type of explicit or implicit behavior (think generated destructors) then no it won't work properly in C.
For example:
// Works fine in C or C++ irrespective of where it was defined
typedef struct {
int field1;
} foo;
// Will have different behavior if it compiles at all.
struct bar {
~bar() {
// destructor code
}
};
typedef struct {
bar b;
} foo;
The C++ struct
declaration is compliant with the C language as long as it does not have any C++ features; a plain, C language, declaration. For example, no private
declarations.