Ok, here is the dilema:
lets say I have class:
class CSomeClass
{
public:
CSomeClass(){};
CSomeClass(){};
void SomeFunctionThatManipulatesSomeString(){ SomeString = "Hello";};
private:
std::string SomeString;
};
And then I have a chunk of memory which is sizeof(CSomeClass), the Memory is allocated for me by some API, so I have no control over the process and I MUST use that chunk of memory, how do I set that chunk of memory to be a CSomeClass object?
The following works to some degree but. . .
// the following line is for demonstration purposes, so
// don''t ask me why I am not using new, this represents the
// part I have no control over, and I think it pretty much helps
// set the scenario
void* memorychunk = malloc(sizeof(CSomeClass));
// and then
((CSomeClass*)memorychunk)->SomeFunctionThatManipulatesSomeString();
... Constructors/Destructors are never called, so string::string is never called for SomeString, and the application crashes right at the assignment, now the following I thought would work:
// the following line is for demonstration purposes, so
// don''t ask me why I am not using new, this represents the
// part I have no control over, and I think it pretty much helps
// set the scenario
void* memorychunk = malloc(sizeof(CSomeClass));
// and then
CSomeClass* pSomeClass = ((CSomeClass*)memorychunk);
*pSomeClass = CSomeClass();
pSomeClass->SomeFunctionThatManipulatesSomeString();
But this one crashes right after the Constructor call, any ideas?
Thanks in advance