Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using C++ classes in C.

Languages

using C++ classes in C.

by  Cagliostro  Posted    (Edited  )
The method is to make external functions with using handles. Handles will be pointers to classes which you will put as an explicitly C++ pointer this. The difference is what in C++ that pointer is hidden. See C++ implementation, like I do it in VisualC++. In GCC should be some similar. You will compille it in C++. In C programs use the .h file and the resulted object file as implementation, not the .cpp. Declarations between
[color blue]#ifdef __cplusplus[/color] and [color blue]#endif[/color] will not be visible from C programs.
The limitation is what you will not be able to overwrite xwrite(void*) function. In my opinion it became a FAQ.
[color blue]
[color green]//classdecl.h[/color]
#if !defined(__CLASSDECL_H)
#define __CLASSDECL_H
#ifdef __cplusplus
class xxx
{
int write();
};
#endif
#ifdef __cplusplus
extern "C" {
#endif
extern int xwrite(void* handle);
#ifdef __cplusplus
}
#endif
#endif
[color green]//end of classdecl.h
//classdecl.cpp[/color]
#include"classdecl.h"
int xxx::write()
{
return 0;
}
int xwrite(void* handle)
{
return ((xxx*)handle)->write();
}
[color green]//end of classdecl.cpp[/color]
[/color]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top