struct BaseType
{
int x1;
float x2;
};
struct ChildType
{
int y1;
float y2;
};
Class Base
{
BaseType obj;
void funcBase(BaseType **ptr)
{
*ptr = &obj; // When this assignment happens ofcourse obj is of the BaseType as the LHS ptr is pointing to a BaseType
Now I want to write a C++ equivalent code of the following 2 algorithmic statements,
BaseType's obj.x1 = ChildTypes's obj.y1;
BaseType's obj.x2 = ChildTypes's.obj.y1;
}
};
class Child :: public Base
{
ChildType obj;
};
I wnat to access child’s obj.y1 from base and assign it to base’s obj.x1.
But 1 thing to remember that object name in base and child is same
“obj”.
Can anyone kindly help me in this. Thanks.
Your question is somewhat vague, but from what I can gather, you want something like this:
Now
childis pointing toobj, as if it were of typeChildType, assuming the cast is successful.Read more about typecasting here.
EDIT: I should just mention that typecasting like this should be avoided if a better design can substitute it. Perhaps you should reconsider your overall design for a moment here.
EDIT2: Given your recent edit, here’s a more specific block of example code: