Board index » cppbuilder » Class types and runtime casting

Class types and runtime casting

I have several components that I have extended with a "Modified" property,
which updates if the user changes anything, along with some other stuff. I
would like to check this property in a handler, but the handler is
registered by several of the different types of components, i.e. my JBEdit
control, my JBMemo control, etc. So, within my handler I have no way to
check the Modified property without doing a switch or if/then block because
I don't knpw what type my Sender is.

Is there a way I can look at the value of my Modified flag in my handler
when I do not know the type of the calling component? Can I cast it to a
dynamically revelealed type? like (hypothetically):

void __fastcall MyCallback(TObject* Sender)
{
  if ( ((classtype(Sender))Sender)->Modified ) {
    DoStuff();
  }

Quote
}

or how about:

if ((dynamic_cast<(typeid(*Sender))>Sender)->Modified) {
    DoStuff();

Quote
}

jon
 

Re:Class types and runtime casting


: "Jon S. Berndt" <jonb @ flowcal. com> wrote:

Quote
>Is there a way I can look at the value of my Modified flag in my handler
>when I do not know the type of the calling component?

dynamic_cast allows you to test the type.

Quote
>if ((dynamic_cast<(typeid(*Sender))>Sender)->Modified) {
>    DoStuff();

A bit complicated.

Try

  TYourBase* Temp;
  Temp = dynamic_cast<TYourBase*>(Sender);

  if (Temp != 0)
    if (Temp->Modified)
      DoSomething();

If your classes do not descend from a common base, you can use RTTI to
determine whether there is a __published property named Modified - see
TypInfo.Hpp.

--
General information:
  * Post to the right group - http://www.borland.com/newsgroups/
    * Do not cross- or multipost
      * Research at http://www.mers.com/searchsite.html

Stefan Hoffmeister - http://www.econos.de/
TeamB - http://www.teamb.com/

Re:Class types and runtime casting


Quote
> If your classes do not descend from a common base, you can use RTTI to
> determine whether there is a __published property named Modified - see
> TypInfo.Hpp.

Ultimately, that is exactly what I need. Thanks, I'll have a look.

Jon

Other Threads