Article View: comp.std.c++
Article #820Re: Dynamic Binding by Argument Type
From: John Townsend
Date: Wed, 06 Feb 1991 14:44
Date: Wed, 06 Feb 1991 14:44
60 lines
2780 bytes
2780 bytes
In article <fuchs.665772943@tmipi4>, fuchs@it.uka.de (Harald Fuchs) writes:
|> johnt@meaddata.com (John Townsend) writes:
|>
|> >Now, consider this:
|>
|> > b1 = &b2;
|> > g(*b1); // Calls g(Base &arg)
|> > b1 = &d1;
|> > g(*b1); // STILL calls g(Base &arg), even though *b1 is a
|> > // Derived object!
|>
|> >Why is it that *b1 is treated as a Derived object when it is the invoking
|> >object, but as a Base object when it is the argument? Is this intentional?
|>
|> Me thinks so. There is no way you could tell g's argument to use
|> static or dynamic binding. What you might want is something like
|> void g (virtual Base&);
|> but that's not C++.
This has occurred to me. The obvious next question is, SHOULD it be C++? Is
there a good reason why this should not be allowed?
|> >It seems somewhat inconsistent to me. Is there some way to dynamically
|> >bind to the overloaded g() based on its argument type?
|>
|> Sure:
|> class Base {
|> public:
|> virtual void f();
|> virtual void g ();
|> }
|>
|> class Derived: public Base {
|> public:
|> void f();
|> void g ();
|> }
|>
|> inline void g (Base& arg) { arg.g (); }
This will work, of course, but in my intended application the g() functions
are already member functions of another class, and must remain so (they
wouldn't make sense as members of Base and Derived, and don't require access
to the representations of these classes).
Again, the problem as I see it is that if b1 points to a Derived object, then
*b1 is treated as a Derived object if it is an invoking object, but as a Base
object in other contexts (such as an argument). It is impossible to externally
use the additional members of a Derived object once it has been assigned to a
Base pointer. If *b1 is a Derived object, I want to be able to pass it to a
function that expects a Derived object and uses those members.
Thanks, Harald, for your thoughts on the subject!
--
John Townsend Internet: johnt@meaddata.com
c/o Mead Data Central UUCP: ...!uunet!meaddata!skibum!johnt
P.O. Box 933 Telephone: (513) 865-7250
Dayton, Ohio, 45401
Technical Details
Message-ID:
<2732@meaddata.meaddata.com>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!tmsoft!torsqnt!hybrid!scifi!bywater!uunet!meaddata!johntReferences:
<2715@meaddata.meaddata.com> <fuchs.665772943@tmipi4>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!mnetor!tmsoft!torsqnt!hybrid!scifi!bywater!uunet!meaddata!johnt From: johnt@meaddata.com (John Townsend) Newsgroups: comp.std.c++ Subject: Re: Dynamic Binding by Argument Type Message-ID: <2732@meaddata.meaddata.com> Date: 6 Feb 91 14:44:57 GMT References: <2715@meaddata.meaddata.com> <fuchs.665772943@tmipi4> Sender: usenet@meaddata.com Reply-To: johnt@meaddata.com (John Townsend) Organization: Mead Data Central, Dayton OH Lines: 60