Topic: Problem about C and C++


Author: cs_phh@ug.cs.ust.hk (Pang Hung Hing Anthony)
Date: Mon, 10 Oct 1994 17:35:46 GMT
Raw View
Hi! I have a problem about using both C and C++ together.

I know how to solve the linkage problem. However, I am not
so sure about the data representation.


// in C++ module

class ww {
// something here
};

extern "C" {
 int test(ww *k);
}

/*
 * in C module
 */

struct ww {
  /* the problem is what should be put in here */
}

int test(struct ww*); /* that is what The C++ programming language 2nd said*/

int main()  {
  struct ww *p
  // something here
  return test(p);
}


Should struct ww have the same data field and in same order as in class ww
in C++ module?  Is it what the standard said? if there is virtual function
in class ww, will the presence of virtual table pointer messy up the corr-
esponding position of the field and I need to add some dummy field to make
it right?
I can't find those information in ARM, The C++  PL 2nd, The D & E of C++.
Of course, I need those solution which is go with the standard.

Please help.

A




Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 10 Oct 1994 15:08:16 -0500
Raw View
cs_phh@ug.cs.ust.hk (Pang Hung Hing Anthony) writes:

>Hi! I have a problem about using both C and C++ together.

>I know how to solve the linkage problem. However, I am not
>so sure about the data representation.


>// in C++ module

>class ww {
>// something here
>};

>extern "C" {
> int test(ww *k);
>}

>/*
> * in C module
> */

>struct ww {
>  /* the problem is what should be put in here */
>}

>int test(struct ww*); /* that is what The C++ programming language 2nd said*/

>int main()  {
>  struct ww *p
>  // something here
>  return test(p);
>}


>Should struct ww have the same data field and in same order as in class ww
>in C++ module?  Is it what the standard said? if there is virtual function
>in class ww, will the presence of virtual table pointer messy up the corr-
>esponding position of the field and I need to add some dummy field to make
>it right?
>I can't find those information in ARM, The C++  PL 2nd, The D & E of C++.
>Of course, I need those solution which is go with the standard.

>Please help.

If you wish to use the same structure in both C and C++ code,
you should declare it as a C language struct everywhere (NO
C++ specific features AT ALL) with the same members. You also
have to use C and C++ compilers that are compatible in terms
of structure padding.

You may be able to get away with having non-virtual methods,
or private members in the C++ declaration, at the risk of
code portability.




Author: panzer@cse.ucsc.edu (John Panzer)
Date: 11 Oct 1994 01:10:48 GMT
Raw View
In article <37c6vg$amb@fsgm01.fnal.gov> b91926@fsgm01.fnal.gov (David Sachs) writes:
>cs_phh@ug.cs.ust.hk (Pang Hung Hing Anthony) writes:
>
>>Hi! I have a problem about using both C and C++ together.
>>I know how to solve the linkage problem. However, I am not
>>so sure about the data representation.
>
>
>>// in C++ module
>
>>class ww {
>>// something here
>>};
>
...
>>/*
>> * in C module
>> */
>
>>struct ww {
>>  /* the problem is what should be put in here */
>>}
...
>>Should struct ww have the same data field and in same order as in class ww
>>in C++ module?  Is it what the standard said? if there is virtual function
>>in class ww, will the presence of virtual table pointer messy up the corr-
>>esponding position of the field and I need to add some dummy field to make
>>it right?
>>I can't find those information in ARM, The C++  PL 2nd, The D & E of C++.
>>Of course, I need those solution which is go with the standard.
>
>>Please help.
>
>If you wish to use the same structure in both C and C++ code,
>you should declare it as a C language struct everywhere (NO
>C++ specific features AT ALL) with the same members. You also
>have to use C and C++ compilers that are compatible in terms
>of structure padding.
>
You could also try having a "struct ww" which is just a plain
struct and is shared by the C and C++ code, and a "class ww_cls"
which inherits from ww and adds non-virtual methods to manipulate
the member data fields.  This should be safe when passing a
"ww_cls*" to anything expecting a "struct ww*", which is the
normal case for me (C++ code calling older C code).

John Panzer