Hello,
I have following problem:
I have an object that belongs to a main thread, and that I want to share with an auxiliary thread. The main thread does calculation on the object and modifies it, and the auxiliary thread simply reads and visualizes that object.
I want the auxiliary thread to be able to:
1. Directly read the main thread object. In that case, the main thread needs to be locked during that time.
2. Read a copy of the main thread object. In that case, the main thread does not need to be locked during that time.
Depending on the user settings, I want to have situation 1 or 2 described above.
Imagine the main thread object to be following class:
class CObject_A
{ // the main thread creates/destroys and uses this object
CObject_A();
virtual ~CObject_A();
void doCalculationAndModifyObject();
CObject_A* duplicateObject();
int data1;
int data2;
};
And imagine the auxiliary thread to have following class declaration:
class CObject_B
{ // the auxiliary thread uses this class. But it does not create/destroy the object.
CObject_B();
virtual ~CObject_B();
void displayObject();
int data1;
int data2;
};
So, is following legal if I do this in the auxiliary thread:
CData_A* objectCreatedFromTheMainThread=getObjectFromMainThread();
CData_B* objectForTheAuxiliaryThread=(CData_B*)objectCreatedFromTheMainThread;
objectForTheAuxiliaryThread->displayObject();
The question is basically: given two different classes that have exactly the same data definition (including order and same compiler), but that have different member functions, is it legal to cast the object from one class to the other?
Thanks for any insight!