Search the web
Sign In
New User? Sign Up
wtl · WTL support list
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Simulated dynamic binding (was RE: [wtl] XML with C++)   Message List  
Reply | Forward Message #5594 of 16191 |
> First, a minor WTL-related design issue:
>
> "template <class _T>
> class CMyXMLBase : public CExpatImpl <_T> "
>
> I can't figure out why everyone keeps doing that. In this case, it's
> probably a doggerel version of the Abstract Template Design Pattern. If so,
> make CExpatImpl into a concrete class and an abstract interface (IExpat, for
> example). Then CMyXMLBase inherits IExpat and resolves any methods we need
> in it, such as OnStartElement or OnEndElement.

Appendix A of ATL Internals talks about this, as well as an article on devx [1].
It's been called "simulated dynamic binding", "ATL style inheritance", "upside
down inheritance", "static polymorphism", and other things. Its used quite a
bit in ATL / WTL for various base classes. Its not mutually exclusive to an
interface, but is often used in conjunction with an interface (such as
IDispatchImpl, IProvideClassInfo2Impl, etc.). Its also the technique used by of
all the "mixin" windowing base classes of ATL / WTL (CWindowImpl, etc.).

Using this approach, in the base class you do something like:

T* pT = static_cast<T*>(this);
pT->OverrideableFunction();

Its essentially like having "OverrideableFunction" be a virtual function, but
with a couple of benefits:

1. It saves at least 2 levels of indirection (with a virtual function pointer
and virtual function table) at run-time.
2. The calculations for a static_cast<> are performed at compile time, so the
code can be optimized better because its a compile time thing rather than a
run-time thing (the compiler might even be able to inline the function call -
which would be impossible if using a virtual function).
3. It can possibly save you from needing a v-table and virtual function
pointer all together - which saves at least the 4 byte virtual function pointer
per instance, plus the size of the virtual table.
4. Your base class doesn't have to define the method - thus acting like a pure
virtual function that has to be defined in derived classes or you get compile
errors.
5. You can call static methods on the derived class. (And technically you
could use public member variables as well - both static and non-static.)
6. Other stuff I'm forgetting I'm sure.

Item #5 can't be done with virtual classes and functions even if you wanted to.
Just one of the many examples of this in ATL is when you are making a COM object
with ATL - you define "BEGIN_COM_MAP", which expands to you defining a handful
of static methods and members that the base class CComObjectRootEx calls to help
implement QueryInterface.

Now that's not to say that every single bit of code using "template <T> class
Base : public T" always do things right, or that the author understood what its
doing. I haven't look at CExpatImpl, so I can't speak to its use. But if you
use the technique correctly, its a good thing IMO.

-Daniel

[1] <http://archive.devx.com/free/mgznarch/vcdj/1999/julmag99/atlinherit1.asp>






Mon Apr 28, 2003 8:32 pm

newob_leinad
Offline Offline
Send Email Send Email

Forward
Message #5594 of 16191 |
Expand Messages Author Sort by Date

... Appendix A of ATL Internals talks about this, as well as an article on devx [1]. It's been called "simulated dynamic binding", "ATL style inheritance",...
dbowen@...
newob_leinad
Offline Send Email
Apr 28, 2003
8:33 pm

... devx [1]. It's been called "simulated dynamic binding", "ATL style inheritance", "upside down inheritance", "static polymorphism", and other things. Just...
Igor Tandetnik
itandetnik
Offline Send Email
Apr 28, 2003
8:38 pm

Although the ATL list used to debate whether Coplien was the first in print, Coplien himself attributes the pattern to others. The article Igor mentioned is ...
Tim Tabor
tltabor
Offline Send Email
Apr 28, 2003
9:13 pm

... Then with Igor online (weilding Coplien) I'm certainly not going to knock it. I have all but called it "simulated dynamic binding" elsewhere. ... These are...
Phlip
phlipcpp
Offline Send Email
Apr 28, 2003
9:11 pm

... Actually, #2 can ALSO catch some bugs at compile-time bug instead of runtime. Marc...
Marc Brooks
anotherexcita
Online Now Send Email
Apr 28, 2003
11:26 pm

Thanks. The first time I read it in ATL Internals I also took it to be an "invention" of the ATL team with VC++ support. Best regards, Paul. ... From: "Igor...
Paul Selormey
pegroups
Offline Send Email
Apr 29, 2003
2:50 am

... Easy enough to draw that "inference". From Appendix A: It is my understanding that the ATL team discovered simulated dynamic binding accidentally. When...
Tim Tabor
tltabor
Offline Send Email
Apr 29, 2003
3:32 am

...and with Forword by Jim Springfield - Inventor of ATL, I believed that stuff in the Appendix till I saw a discussion on it at CodeProject :(( On the...
Paul Selormey
pegroups
Offline Send Email
Apr 29, 2003
4:30 am

... there's no denying that this approach is optimized for runtime speed, plus it has a curiosity value for impressing laymen/bystanders :) however how about...
Nikos Bozinis
umeca74
Offline Send Email
Apr 29, 2003
7:32 am

On point 2. One of the main advantages of not having traditional inheritance is the ability to have a "derived" CWindow which is say a "blue" window, and...
Gordon Smith
schmoo2k
Online Now Send Email
Apr 29, 2003
1:28 pm

I must say that the non-traditional inheritance (dynamic binding) has allowed us to create a really cool skinning engine at our company. We take full advantage...
Scott Andrew
scottandrew
Offline Send Email
Apr 29, 2003
5:04 pm

... speed, ... it ... The thing is, the code that's generated is typically on the order of a few lines, and is a prime candidate for being inlined in the first...
spectecjr
Offline Send Email
Apr 29, 2003
10:13 pm

... the problem is down to templated classes, not inlining. Read any book on templates and it will warn you on the code duplication side-effect they suffer....
Nikos Bozinis
umeca74
Offline Send Email
Apr 30, 2003
10:22 am

... I thought a Skin was a canonical example of the Decorator Design Pattern (which is also the "Pass the Buck" Pattern ;-) Props for finding a clean way to...
Phlip
phlipcpp
Offline Send Email
Apr 29, 2003
6:14 pm

One thing, make sure that you test with the link flags: /OPT:REF and /OPT:ICF. They are claimed to make differences with ATL/WTL, but it's been ages since I...
Andreas Magnusson
zune01
Offline Send Email
Apr 30, 2003
11:58 am

... and ... i have /opt:ref and :nowin98 allright icf doesn't make a difference to my knowledge...
Nikos Bozinis
umeca74
Offline Send Email
Apr 30, 2003
12:41 pm

1. Any compiler/linker worth its salt will remove duplicate template code, better ones will also remove extra template code generated from templated classes...
McDonald, Andrew
bfhobbes
Offline Send Email
May 2, 2003
3:53 pm

... code, better ones will also remove extra template ... arguments but which result in the same machine code. I believe VC ... article than detailed this...
Igor Tandetnik
itandetnik
Offline Send Email
May 3, 2003
12:38 am

... The as-if rule implies a C++ compiler must produce a program that behaves as-if it had followed the virtual machine rules in the C++ Standard. If I call...
Phlip
phlipcpp
Offline Send Email
May 3, 2003
12:52 am

... f<long> ... linker ... when we ... will ... My sample does not try to call the function and detect from inside the function which instantiation is being...
Igor Tandetnik
itandetnik
Offline Send Email
May 3, 2003
1:22 am

... My ability to confuse the issue has struct again. assert(pf1 != pf2) will not guarantee f<int>() doesn't secretly call f<long>() (or vice versa) when you...
Phlip
phlipcpp
Offline Send Email
May 3, 2003
1:43 am

... well-defined ... Ah, I see. I guess this is a valid optimization, does not seem to be anything wrong with it. Does any actual compiler/linker behave this...
Igor Tandetnik
itandetnik
Offline Send Email
May 3, 2003
2:01 am

... I have pushed us well outside the envelop of my C++ awareness. ;-) -- Phlip http://www.c2.com/cgi/wiki?TestFirstUserInterfaces...
Phlip
phlipcpp
Offline Send Email
May 3, 2003
2:11 am

... way, ... and ... do ... Well, maybe Andrew McDonald knows. I would really like to find out exactly which compilers are better than those worth their salt....
Igor Tandetnik
itandetnik
Offline Send Email
May 3, 2003
2:20 am

... I don't care. My sane subset (which now includes SDB) is much narrower than my envelop, and already tuned for such optimizations as matter to real ...
Phlip
phlipcpp
Offline Send Email
May 3, 2003
2:24 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help