Re: Hmm... inheritence... hmmm

From: Siemel Naran (SiemelNaran_at_REMOVE.att.net)
Date: 10/19/04


Date: Tue, 19 Oct 2004 03:15:40 GMT


"JKop" <NULL@NULL.NULL> wrote in message news:4AMcd.37317

> I know the following would work, but it seems a bit inefficent to me:
>
> #include <typeinfo>
>
> virtual void Mate(Mammal &mammal)
> {
> //Wait a minute, I'm not mating unless
> //it's with another dog!
>
> try
> {
> Dog& doggy = dynamic_cast<Dog&>(mammal);
>
> //Now perform mating
> }
> catch( std::bad_cast )
> {
>
> }
> }
>
>
> Any thoughts on this?

Why do you think the above is inefficient? You can implement double
dispatch, but it would probably be slower than the use of dynamic_cast.

> Dog& doggy = dynamic_cast<Dog&>(mammal);

The above is not symmetric. The line does not throw an exception if mammal
is a class Dog or any derived from it. This allows a dog to mate with
another class that is derived from dog. Thus

    dog.mate(deriveddog);

would work without throwing a bad_cast exception. Yet deriveddog.mate(dog)
would throw a bad_cast exception.

Maybe the correct thing is to use typeid.

As a matter of style, I'd make Mate a non-member function that checks if the
types are compatible, then calls the virtual function of the class, which
should be private or protected. These virtual functions may assume that the
types are compatible, and can use static_cast to convert a Mammal& to a
Dog&.

void mate(const Mammal& lhs, const Mammal& rhs) {
   assert(typeid(lhs) != typeid(rhs));
   return lhs.mate(rhs);
}

void Dog::mate(const Mammal& rhs) {
   const Dog& that = static_cast<const Dog&>(rhs);
}



Relevant Pages

  • [patch v2] x86: Add testcases for RODATA and NX protections/attributes
    ... +config DEBUG_RODATA_TEST ... this implies having dynamic exception handling table entries. ... +static void test_exit ...
    (Linux-Kernel)
  • Re: Object reference not set to an instance of an object.
    ... > The event handler WTGDataRowReady10Handler is called by a delegate in the ... > catch (Exception ex) ... > private void WTGDataRowReady10Handler( ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: N1298 - try/finally for C
    ... Using a void*, or something containing a void*, gives ... Then we have a function that potentially throws an exception: ... If you mean some custom allocator, then we generally need to pass yet ... As for function pointers, and besides that throwing function pointers ...
    (comp.std.c)
  • [RFC] Improved versioned pointer algorithms
    ... The orphan test is used in snapshot write and exception delete to ... The current method of determining whether a ghost exception is an ... its child count then the ghost exception is an orphan, ... void set_bit ...
    (Linux-Kernel)
  • RE: Custom permission
    ... exception directly, but to make it rasing if someone in the deeper frame ... void Bar() ... permission is not granted, expection will be raised, too. ... | From: "Marcelo Palladino" ...
    (microsoft.public.dotnet.security)