Casting from within a class
-
Is there an easy way to get a subclass out of a class from within the class? This would be easiest explained in code ;P class A; class B: public A; From within B, I need to get a temporary variable of the subclass A. The subclass is setup with void operator=(A& a); None of them are pointers, just ... can't remember the term right now... I access them with a . not a -> man my memory is going... Anyway, I've tried this: A a; a = (A)this; and it of course doesn't work. How can I grab the data from the subclass? What I'm doing is implementing a rotation function for the nice CPolygon class that Chris wrote (*very* nice class btw). And I want to make a temp copy of the CPolygon data so I don't loose the data as the poly rotates. (If you rotate a polygon to much it colapses in on itself due to inaccuracy) So I want to make a backup copy of the data, rotate that data and then use it. But I can't grab the data... What do I do?!? Argh! Programming in binary is as easy as 01 10 11.
-
Is there an easy way to get a subclass out of a class from within the class? This would be easiest explained in code ;P class A; class B: public A; From within B, I need to get a temporary variable of the subclass A. The subclass is setup with void operator=(A& a); None of them are pointers, just ... can't remember the term right now... I access them with a . not a -> man my memory is going... Anyway, I've tried this: A a; a = (A)this; and it of course doesn't work. How can I grab the data from the subclass? What I'm doing is implementing a rotation function for the nice CPolygon class that Chris wrote (*very* nice class btw). And I want to make a temp copy of the CPolygon data so I don't loose the data as the poly rotates. (If you rotate a polygon to much it colapses in on itself due to inaccuracy) So I want to make a backup copy of the data, rotate that data and then use it. But I can't grab the data... What do I do?!? Argh! Programming in binary is as easy as 01 10 11.
A is the parent class of B, not the subclass. A pointer to B can always be cast to a pointer to A, so just do:
void B::SomeFunc()
{
A& refa = *(A*) this;// use refa.whatever() here...
}--Mike-- My really out-of-date homepage Buffy's on. Gotta go, bye! Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.
-
A is the parent class of B, not the subclass. A pointer to B can always be cast to a pointer to A, so just do:
void B::SomeFunc()
{
A& refa = *(A*) this;// use refa.whatever() here...
}--Mike-- My really out-of-date homepage Buffy's on. Gotta go, bye! Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.
-
A is the parent class of B, not the subclass. A pointer to B can always be cast to a pointer to A, so just do:
void B::SomeFunc()
{
A& refa = *(A*) this;// use refa.whatever() here...
}--Mike-- My really out-of-date homepage Buffy's on. Gotta go, bye! Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.
this would have worked.
A a;
a = *this;Todd Smith