Re: Using STL(map) inside of C data structure. How?
Hi,
In order to work, you would have to replace the malloc call with
new[]. That way, the map constructor will be called.
If your question was in the context of mixing C with C++ code, the
answer is more complex.
1- You must write the main function in a C++ TU to make sure that C++
runtime environment is setup correctly.
2- Create a TU presenting a C interface for manipulating sType:
sType.h
#ifdef __cplusplus
extern "C"
{
#endif
typedef struct {
void *m; /* Must be type less as you cannot include a C++ template
class declaration in C modules
} sType;
/* C style constructor/destructor */
sType *create_sType();
destroy_sType(sType *);
use_sType(sType *);
#ifdef __cplusplus
}
#endif
sType.cpp
inline std::map<const char*, int> *getMap(sType *s)
{
return static_cast<std::map<const char*, int> *>(s->m);
}
// You must wrap your C functions entry points to be sure that
// C++ exceptions do not cross into C modules.
sType *create_sType()
{
try
{
sType *res = new sType;
res->m = std::map<const char*, int>;
return res;
}
catch(...)
{
}
return NULL;
}
destroy_sType(sType *s)
{
delete getMap(s);
delete s;
}
/*
* For every methods that need to use the map, use the inline function
getMap() to retrieve
* the map from the sType POD.
*/
use_sType(sType *s)
{
}
Greetings,
Olivier Langlois
http://www.olivierlanglois.net
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]