[comp.lang.c++] CastDown vs. Virtuals

aaron@rruxh.UUCP (Akman) (06/08/90)

I keep seeing notes about C++'s inability to do CastDowns from the
base class to the derived class.  Can someone explain the case(s)
where virtual functions in the base class are insufficient (I am
trying to follow the discussion, but it still seems to me that
they're sufficient).  What I mean by virtual functions in the base
class is:

	class animal {
		// biology...

	public:
		// ...
		virtual int walk()          { cout < "not defined\n"; }
		virtual int make_my_noise() { cout < "not defined\n"; }
		// ...
	};

	class dog : animal {
		// dog biology...

	public:
		// ...
		int walk()        { cout << "all fours, tail wagging\n"; }
		int make_my_noise { cout << "woof woof\n"; }
	};

As long as there is a generic base class that describes the mutual
properties of all possible derived classes, that should be
sufficient (and object oriented).  Yes?  No?
--

-----------
Aaron Akman, 201-699-8019, bellcore!rruxh!aaron, RRC 4D-728

vaughan@mcc.com (Paul Vaughan) (06/09/90)

	As long as there is a generic base class that describes the mutual
	properties of all possible derived classes, that should be
	sufficient (and object oriented).  Yes?  No?

Not quite.  The problem is that there will be properties of some
possible derived classes that you will want to access that are not
mutual properties of all possible derived classes.  If you have code
that works with the base class and passes these base class pointers to
code that really wants to assume more so that it can access these more
specific properties, then you will need to cast down (and perhaps
first verify that the cast is valid).  If you had omitted the word
"mutual," then it would be sufficient, but not object oriented.

	

 Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639
 Box 200195, Austin, TX 78720  | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan

roger@procase.UUCP (Roger H. Scott) (06/12/90)

In article <AARON.90Jun8125224@rruxh.rruxh.UUCP> aaron@rruxh.UUCP (Akman) writes:
> Can someone explain the case(s) where virtual functions in the base class are
> insufficient?
>
>	class animal {
>		// biology...
>
>	public:
>		// ...
>		virtual int walk()          { cout < "not defined\n"; }
>		virtual int make_my_noise() { cout < "not defined\n"; }
>		// ...
>	};
>
>	class dog : animal {
>		// dog biology...
>
>	public:
>		// ...
>		int walk()        { cout << "all fours, tail wagging\n"; }
>		int make_my_noise { cout << "woof woof\n"; }
>	};
>

Defining virtual functions in base classes to produce "errors" amounts to 
run-time type checking.  If class animal is an *abstract* class (meaning there
will never be any objects of that class) then the virtual functions should
be made "pure virtual".  If class animal is to have instances then all of
its member functions should be meaningful (if not necessarilly useful) to call.
In cases like this example, where the message is a request for something
to be done with no meaningful return value, it is easy to give a meaningful
interpretation to the messages in the base class - just do nothing.  Things
get a little messier when the message is supposed to produce some sort of
result.
This sort of problem is part of the reason that untyped OO languages like
Smalltalk seem in some ways "cleaner" than typed OO langauges like C++.
@
@
@
@
@
@
@
@
@