I understand that Brandon's example is illustrative of a technique and not
intended to show the absolute best way to solve a problem. However, I also
would like to show that there are other ways to solve the particular problem
he uses in his example. As he said in his first response: "I'd really
reccommend coding in such a way that you don't need [RTTI]."
For instance, you could have:
class Customer {
...
virtual double GetPriceToCharge(double price) { return price; }
...
};
class WholesaleCustomer : public Customer {
...
};
class RetailCustomer : public Customer {
...
virtual double GetPriceToCharge(double price) { return price *
markup; }
...
};
Customer *cust = new RetailCustomer(); // as opposed to WholesaleCustomer
....
SetPrice(cust->GetPriceToCharge(base_price);
This shows how you can do the same thing without using RTTI. I'm just
pointing this out because I don't know Paulo's actual problem, and would
like to demonstrate how to turn an RTTI-base solution into a non-RTTI-based
solution.
-- Keith
> -----Original Message-----
> From: Brandon Roberson
> Sent: Monday, February 14, 2005 12:53 PM
> To: Palm Developer Forum
> Subject: Re: Type Identification.
>
> This is where the "dynamic_cast" operator comes into play,
> allowing you to attempt casting something as something else.
> For example:
>
> Customer *cust = new RetailCustomer(); // as opposed to
> WholesaleCustomer
>
> ....
>
> if (dynamic_cast <RetailCustomer *> (cust) ) {
> // this is a retail customer, or is derived from it
> SetPrice(base_price * markup);
> }
> else if (dynamic_cast <WholesaleCustomer *> (cust)) {
> SetPrice(base_price);
> }
>
> This allows you to come back later and derive
> NonProfitRetailCustomer from RetailCustomer, handling the
> fact that this type of customer is exempt from sales tax, and
> you don't have to mess with all of the other routines, since
> a pointer to a NonProfitRetailCustomer object can
> successfully be cast to a RetailCustomer pointer using dynamic_cast.
>
> The dynamic_cast operator costs more in terms of execution
> speed as your inheritance hierarchy grows, but this is the
> real "coolness" of RTTI.
--
For information on using the Palm Developer Forums, or to unsubscribe, please
see http://www.palmos.com/dev/support/forums/