A common C idiom is to declare a struct in this form:
typedef struct {
int elementA;
int elementB;
} MyStruct;
This idiom allows the C programmer to just type "MyStruct"
in their C program instead of typing "struct MyStruct". It
gives MyStruct a few of the behaviors of a C++ class or
struct. The idiom is funny, though, because you end up
with a typedef of an anonymous struct. The struct has no
name; only the typedef has a name.
I'm writing a C++ program that uses some structs defined in
a C header with this C idiom of definition. I'd prefer not to
mess with the C header file if I can avoid it.
My problem is that I want to forward declare some of these
structs so I don't have to #include the C header file in my
C++ header file. By doing the forward declaration in my
C++ header file I would only need to include the C file in
my implementation. I want to keep the C-style structs from
spreading into the rest of my system.
When I try a forward declaration like this:
struct MyStruct;
I get an appropriate error message from my compiler:
conflicting types for `typedef struct MyStruct MyStruct'
previous declaration as `struct MyStruct'
I can't figure out any way to get the forward declaration
to work without changing the C header file to give the
struct a real name.
Advice?
--------------------------------------
Scott Schurr
Integrated Measurement Systems, Inc.
Voice: (503) 626-7117
Fax: (503) 644-6969
Email: sco...@ims.com
--------------------------------------
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
Scott Schurr wrote:
>
> My question relates to using C header files in C++.
>
> A common C idiom is to declare a struct in this form:
>
> typedef struct {
> int elementA;
> int elementB;
> } MyStruct;
>
>
> When I try a forward declaration like this:
>
> struct MyStruct;
>
> I get an appropriate error message from my compiler:
>
> conflicting types for `typedef struct MyStruct MyStruct'
> previous declaration as `struct MyStruct'
>
> I can't figure out any way to get the forward declaration
> to work without changing the C header file to give the
> struct a real name.
>
You can't do it that way, even in C.
typedef struct struct_tag {
int elementA;
int elementB;
} MyStruct;
could be forward declared (either language) as:
typedef struct struct_tag MyStruct;
but I'm not sure how to handle it for a struct with no type
name. However, your compiler hints that maybe it gave it
the name of the typedef (this isn't standard):
typedef struct MyStruct MyStruct;
might work as a forward delcartion.
Or you can use the same name for the struct and the typedef:
typedef struct MyStruct { ... } MyStruct;
Now you can use "struct MyStruct" as well as "MyStruct" in both C and C++,
and forward-declare it as
struct MyStruct;
in either language.
But you cannot forward-declare a struct unless it has a tag.
--
Steve Clamage, stephen...@sun.com
Thanks for the help, gentlemen. Since I'm trying to avoid
messing with the contents of the C header, I came up with
the following solution.
The C header file contains
typedef struct {
int elementA;
int elementB;
} MyStruct;
I can't forward declare MyStruct. So instead I forward declare
struct MyStruct_wrapper;
Then later, in my C++ implementation, I can define MyStruct_wrapper
as
struct MyStruct_wrapper {
MyStruct m;
};
While this wouldn't work for every circumstance, it works for
mine today because my goal is to keep the definition of MyStruct
out of my header.
I'm not sure why I didn't think of this solution sooner.
Thanks again for the help.
--------------------------------------
Scott Schurr
Integrated Measurement Systems, Inc.
Voice: (503) 626-7117
Fax: (503) 644-6969
Email: sco...@ims.com
--------------------------------------
Sent via Deja.com
http://www.deja.com/
Nice solution. But why not:
struct MyStruct_wrapper : public MyStruct {};
No need to add m. everywhere in your code I think...
--
Ivan Vecerina - Surgical Navigation Network
--
Brainbench MVP for C++
http://www.brainbench.com
Cool. And it passes the "isa" test. Thanks very much. An
excellent solution.
--------------------------------------
Scott Schurr
Integrated Measurement Systems, Inc.
Voice: (503) 626-7117
Fax: (503) 644-6969
Email: sco...@ims.com
--------------------------------------
Sent via Deja.com
http://www.deja.com/
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]