On Apr 29, 12:00 am, "Hak...@[EMAIL PROTECTED]
" <Hak...@[EMAIL PROTECTED]
> wrote:
> I have two issues.
>
> 1) Given the objects item1 and item2, derived from type T, I need to
> find out if item2 is derived from item1.
Presumably, you mean: the type of item2 is derived from the type of item1.
Objects are not derived from other objects, but cl***** are.
Objects are instances of cl*****. This distinction is im****tant, and it
seems
from your problem like you are confusing the two.
> I solved this by learning (by accident) of the typeof() keyword...that
> is apparently not standard. Nevertheless, it allowed me to write the
> following line of code that presumably works...if only it would
> compile.
>
> > if( dynamic_cast< typeof( item1 ) >( item2 ) );
>
> Interestingly enough,
>
> > cout << dynamic_cast< typeof( item1 ) >( item2 );
>
> does compile.
if (x) ... requires that x is a boolean or converts to one. cout << x does
not require that at all. Now, what you probably wanted was
if (dynamic_cast<typeof(&item1)>(&item2) != 0) { }
The pointer comparison is a boolean expression.
A cleaner solution without typeof is to turn this test into a function.
template<typename T1, typename T2>
bool is_derived_obj(T1, T2 const& t2) {
return dynamic_cast<T1*>(&t2) != 0;
}
HTH,
Michiel Salters
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|