[comp.lang.c++] mixing c++ and c - SUMMARY

robert@ireq.hydro.qc.ca (R.Meunier 8516) (02/07/91)

	Contrary to what most people think, it is possible to mix c++
function with c. Here is how i have done it.


-------------------------------------------------------------
class A
{
private:
	int	i;
public:
	A(int);
	int inc();
}

A::A(int ii)
{
	i=ii;
}

A::inc()
{
	return(++i);
}

caddr_t newA(int ii)
{
	return(new A(ii));
}

int	incA(A *a)
{
	return(a->inc());
}
-------------------------------------------------------------

That code is compile with a c++ compiler and put in a library

	If i need that class in a standard c program, i call only
the function newA() and incA(). Their name is not mangle since
they are standard function. The c++ library libC.a as to be
provide to the compiler and someone have mention by mail that
the function _main() as to be called manually to initialise
static variables in classes.

	I did not search very much but 'cout' were crashing my program,
since i didn't need them, i took them out.

Thank you to everyone who have replied 


--
Robert Meunier                     Institut de Recherche d'Hydro-Quebec
Ingenieur                          1800 Montee Ste-Julie, Varennes
Internet: robert@ireq.hydro.qc.ca  Qc, Canada, J3X 1S1 
maintainer: BASIC mailing list request to basic-request@ireq.hydro.qc.ca

fuchs@it.uka.de (Harald Fuchs) (02/09/91)

robert@ireq.hydro.qc.ca (R.Meunier 8516) writes:

>	Contrary to what most people think, it is possible to mix c++
>function with c. Here is how i have done it.

>That code is compile with a c++ compiler and put in a library

With which C++ compiler?

>	If i need that class in a standard c program, i call only
>the function newA() and incA(). Their name is not mangle since
>they are standard function.

They _should_ be mangled unless you specify
  extern "C" {
    caddr_t newA (int);
    int incA (A*);
  }
Seems that you have an obsolete C++ compiler.

>The c++ library libC.a as to be
>provide to the compiler and someone have mention by mail that
>the function _main() as to be called manually to initialise
>static variables in classes.

The problem lies in global (extern) objects with constructors and/or
destructors. If your extern "C" functions use them, you lose.
--

Harald Fuchs <fuchs@telematik.informatik.uni-karlsruhe.de>
<fuchs@telematik.informatik.uni-karlsruhe.dbp.de>   *gulp*