C ++ runtime type check

Source: Internet
Author: User

# Include <afx. h>

Class animal: Public cobject
{
Declare_dynamic (animal)
};

Class dog: Public animal
{
Declare_dynamic (DOG)
};

Class Cat: Public animal
{
Declare_dynamic (CAT)
};

Class cfoo
{
Public:
Void animalsays (animal *);
};

Int main (INT argc, char * argv [])
{
Dog Rex;
Cat Kitty;
Cfoo Foo;

Foo. animalsays (& Rex );
Foo. animalsays (& Kitty );

Return 0;
}

Implement_dynamic (animal, cobject)
Implement_dynamic (dog, animal)
Implement_dynamic (CAT, animal)

Void cfoo: animalsays (animal * panimal)
{
If (panimal-> iskindof (runtime_class (DOG )))
Printf ("bark! ");
Else if (panimal-> iskindof (runtime_class (CAT )))
Printf ("miaou! ");
}
 

Link: Accessing run-time class information
Deriving a class from cobject

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.