Article View: comp.std.c++
Article #813Dynamic Binding by Argument Type
From: John Townsend
Date: Tue, 05 Feb 1991 16:09
Date: Tue, 05 Feb 1991 16:09
47 lines
1757 bytes
1757 bytes
Suppose that you have the following classes:
class Base {
public:
virtual void f();
}
class Derived: public Base {
public:
virtual void f();
}
and, elsewhere, you have the following overloaded functions:
void g (Base &arg);
void g (Derived &arg);
Clearly, dynamic binding can be used as follows:
Base *b1, b2;
Derived d1;
b1 = &b2;
b1->f(); // Calls f() in Base class
(*b1).f(); // Calls f() in Base class
b1 = &d1;
b1->f(); // Calls f() in Derived class
(*b1).f(); // Calls f() in Derived class
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?
It seems somewhat inconsistent to me. Is there some way to dynamically bind
to the overloaded g() based on its argument type?
--
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:
<2715@meaddata.meaddata.com>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!sdd.hp.com!spool.mu.edu!uunet!meaddata!johntShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!sdd.hp.com!spool.mu.edu!uunet!meaddata!johnt From: johnt@meaddata.com (John Townsend) Newsgroups: comp.std.c++ Subject: Dynamic Binding by Argument Type Message-ID: <2715@meaddata.meaddata.com> Date: 5 Feb 91 16:09:42 GMT Sender: usenet@meaddata.com Reply-To: johnt@meaddata.com (John Townsend) Organization: Mead Data Central, Dayton OH Lines: 47