<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx</link><description>A co-worker came by to ask what he thought was a coding "style" question that turned into a correctness issue, and I thought I'd share it. Someone had defined two COM interfaces: interface IFoo : IUnknown { HRESULT FooMethod1(); HRESULT FooMethod2();</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7727095</link><pubDate>Sat, 16 Feb 2008 05:37:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7727095</guid><dc:creator>RyanBemrose</dc:creator><description>&lt;p&gt;A class that &amp;quot;implements IFoo and IFoo2&amp;quot; is hopefully just inheriting IFoo2, since that necessarily requires implementing IFoo. &amp;nbsp;Every IFoo2 is also an IFoo, and if you have an IFoo2*, then you can cast to IFoo* with impunity, thanks to the magic of single inheritance.&lt;/p&gt;
&lt;p&gt;If you have an IFoo*, the only way to make it into an IFoo2* is QueryInterface(), which is, I think, the point of this post.&lt;/p&gt;
&lt;p&gt;If there's a style question in this post, it has to do with how to do the cast. &amp;nbsp;I suspect the whole reason the question came up is because the co-worker was using C-style casts, and although (IFoo2*)pFoo is wrong, the compiler will let you do it. &amp;nbsp;If you try to use static_cast&amp;lt;IFoo2*&amp;gt;(pFoo), the compiler will throw an error, exactly because this is wrong.&lt;/p&gt;
</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7727386</link><pubDate>Sat, 16 Feb 2008 05:58:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7727386</guid><dc:creator>Gwyn</dc:creator><description>&lt;p&gt;I suspect that this also has something to do with why CreateObject calls in COM take a parameter specifying the interface of the object that you want to use. In this case, GetFoo should really be something like:&lt;/p&gt;
&lt;p&gt;HRESULT GetFoo(IUnknown **ppFoo, IID *piid)&lt;/p&gt;
&lt;p&gt;Then, the caller could specify that they wanted an IFoo2 explicitly, and if that wasn't supported, they would get an E_NO_INTERFACE&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7727994</link><pubDate>Sat, 16 Feb 2008 06:50:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7727994</guid><dc:creator>SvenGroot</dc:creator><description>&lt;p&gt;Actually, in general given a class CFoo : IFoo2, and IFoo2 : IFoo, if you have a IFoo* that you know is actually a CFoo, it's perfectly safe to do static_cast&amp;lt;IFoo2*&amp;gt;(p). The vtable will be adjusted on the cast so it'll work fine. If you're not sure the pointer is an IFoo2 you could use a dynamic_cast which throws an exception if it isn't.&lt;/p&gt;
&lt;p&gt;In the context of COM however, you shouldn't assume that's how the interfaces are implemented, and of course you should use QueryInterface.&lt;/p&gt;
&lt;p&gt;Speaking of style, I hope nobody still actually prefixes their classes with a C. :P&lt;/p&gt;
</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7728276</link><pubDate>Sat, 16 Feb 2008 07:15:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7728276</guid><dc:creator>Jack Mathews</dc:creator><description>&lt;p&gt;Based on the code you've provided, I'm afraid that your layout for the class in incorrect.&lt;/p&gt;
&lt;p&gt;You gave us the following assumptions:&lt;/p&gt;
&lt;p&gt;IFoo : IUnknown and IFoo2 : IFoo&lt;/p&gt;
&lt;p&gt;Now you mentioned that CFoo implements both interfaces. &amp;nbsp;However, since IFoo2 inherits from IFoo, that means that it's:&lt;/p&gt;
&lt;p&gt;CFoo : IFoo2&lt;/p&gt;
&lt;p&gt;Since this is a single-inheritance chain, then adjustor thunks are not necessary, so for a CFoo, IFoo and IFoo2 can (and do) share the same vtable. &amp;nbsp;The one and only vtable is laid out the way that you laid out the IFoo2 vtable. &amp;nbsp;The class will have one vtbl pointer at the top followed by the data for CFoo.&lt;/p&gt;
&lt;p&gt;Because of the single inheritance, all casts can and will work just fine. &amp;nbsp;Now I do agree that _depending_ on this is a bad thing, because if IFoo2 were broken out to have no inheritance from IFoo, then the class looks closer to what you've described. &amp;nbsp;Since QueryInterface works no matter what, it is, all in all, the right solution in the long run. &amp;nbsp;But your friend's code was in no danger of crashing.&lt;/p&gt;
&lt;p&gt;RyanBemrose:&lt;/p&gt;
&lt;p&gt;You're wrong. &amp;nbsp;It's a perfectly valid static_cast to go from IFoo* to IFoo2*. &amp;nbsp;The only time it wouldn't be a valid static_cast would be if there were multiple copies of a base class and you tried to cast to a subclass. &amp;nbsp;For instance, if you had, B:A, C:A, and D:C,B, then you can't cast an A to a D, because there are two copies of A in D.&lt;/p&gt;
&lt;p&gt;SvenGroot:&lt;/p&gt;
&lt;p&gt;&amp;quot;The vtable will be adjusted on the cast so it'll work fine.&amp;quot; - There is no vtable adjustment in this case, since there's only one vtable and the compiler knows it.&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7728360</link><pubDate>Sat, 16 Feb 2008 07:21:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7728360</guid><dc:creator>Mark Sowul</dc:creator><description>&lt;p&gt;Why not change IBar's GetFoo to return an IFoo2? (side note: I hate using foo and bar) - since IFoo2 is an IFoo and the team owns both interfaces, shouldn't that work with little issue?&lt;/p&gt;
&lt;p&gt;(Unless other callers are doing similar casting monkey business)&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7730451</link><pubDate>Sat, 16 Feb 2008 10:04:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7730451</guid><dc:creator>LarryOsterman</dc:creator><description>&lt;p&gt;Sven: Actually lots of Windows code prefixes their classes with C - it's a legacy of the old hungarian days.&lt;/p&gt;
&lt;p&gt;And the cast isn't safe unless the &amp;lt;i&amp;gt;compiler&amp;lt;/i&amp;gt; can tell that it's a CFoo - otherwise it has to assume that it's a class with nothing but a vtable.&lt;/p&gt;
</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7730459</link><pubDate>Sat, 16 Feb 2008 10:05:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7730459</guid><dc:creator>LarryOsterman</dc:creator><description>&lt;p&gt;Mark: Because that would change the interface contract for IBar, which means that IBar's interface ID would have to change.&lt;/p&gt;
</description></item><item><title>Don't believe me?  Just ask Larry.</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7730591</link><pubDate>Sat, 16 Feb 2008 10:16:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7730591</guid><dc:creator>Noticias externas</dc:creator><description>&lt;p&gt;QueryInterface isn&amp;amp;#39;t a particularly controversial topic (and I haven&amp;amp;#39;t heard any grumbling about&lt;/p&gt;
</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7736672</link><pubDate>Sat, 16 Feb 2008 18:56:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7736672</guid><dc:creator>Jack Mathews</dc:creator><description>&lt;p&gt;&amp;quot;And the cast isn't safe unless the &amp;lt;i&amp;gt;compiler&amp;lt;/i&amp;gt; can tell that it's a CFoo - otherwise it has to assume that it's a class with nothing but a vtable.&amp;quot;&lt;/p&gt;
&lt;p&gt;Right, but given the inheritance tree, that doesn't matter. &amp;nbsp;Your friend's code was never in danger of crashing so long as the class hierarchy matches the assumptions.&lt;/p&gt;
&lt;p&gt;I'm sorry, but this whole entry is incorrect. &amp;nbsp;I mean, given this code:&lt;/p&gt;
&lt;p&gt;---&lt;/p&gt;
&lt;p&gt;class IFoo : public IUnknown&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;public:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;virtual HRESULT FooMethod1() = 0;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;virtual HRESULT FooMethod2() = 0;&lt;/p&gt;
&lt;p&gt;};&lt;/p&gt;
&lt;p&gt;class IFoo2 : public IFoo&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;public:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;virtual HRESULT Foo2Method1() = 0;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;virtual HRESULT Foo2Method2() = 0;&lt;/p&gt;
&lt;p&gt;};&lt;/p&gt;
&lt;p&gt;class CFoo : public IFoo2&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;public:&lt;/p&gt;
&lt;p&gt;	HRESULT STDMETHODCALLTYPE QueryInterface(const IID &amp;amp;,void **) { return E_NOTIMPL; }&lt;/p&gt;
&lt;p&gt;	ULONG STDMETHODCALLTYPE AddRef(void) { return 0; }&lt;/p&gt;
&lt;p&gt;	ULONG STDMETHODCALLTYPE Release(void) { return 0; }&lt;/p&gt;
&lt;p&gt;	HRESULT FooMethod1() { return E_NOTIMPL; }&lt;/p&gt;
&lt;p&gt;	HRESULT FooMethod2() { return E_NOTIMPL; }&lt;/p&gt;
&lt;p&gt;	HRESULT Foo2Method1() { return E_NOTIMPL; }&lt;/p&gt;
&lt;p&gt;	HRESULT Foo2Method2() { return E_NOTIMPL; }&lt;/p&gt;
&lt;p&gt;private: &lt;/p&gt;
&lt;p&gt;	int mI;&lt;/p&gt;
&lt;p&gt;};&lt;/p&gt;
&lt;p&gt;void main()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	CFoo foo;&lt;/p&gt;
&lt;p&gt;	printf( &lt;/p&gt;
&lt;p&gt;		&amp;quot;Size: %d\n&amp;quot;&lt;/p&gt;
&lt;p&gt;		&amp;quot;CFoo *: %x IFoo2 *:%x IFoo *:%x\n&amp;quot;,&lt;/p&gt;
&lt;p&gt;		sizeof( CFoo ),&lt;/p&gt;
&lt;p&gt;		&amp;amp;foo, static_cast&amp;lt;IFoo2 *&amp;gt;( &amp;amp;foo ), static_cast&amp;lt; IFoo * &amp;gt;(&amp;amp;foo) );&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;---&lt;/p&gt;
&lt;p&gt;You get this output:&lt;/p&gt;
&lt;p&gt;Size: 8&lt;/p&gt;
&lt;p&gt;CFoo *: 12ff28 IFoo2 *:12ff28 IFoo *:12ff28&lt;/p&gt;
&lt;p&gt;It's clear that all casts work, all are based from the same address, and there is only one vtable.&lt;/p&gt;
&lt;p&gt;Now, if I would have changed the class decls to:&lt;/p&gt;
&lt;p&gt;class IFoo : public virtual IUnknown&lt;/p&gt;
&lt;p&gt;class IFoo2 : public virtual IUnknown&lt;/p&gt;
&lt;p&gt;class CFoo: public IFoo, public IFoo2&lt;/p&gt;
&lt;p&gt;then you get this:&lt;/p&gt;
&lt;p&gt;Size: 24&lt;/p&gt;
&lt;p&gt;CFoo *: 12ff18 IFoo2 *:12ff20 IFoo *:12ff18&lt;/p&gt;
&lt;p&gt;Which is somewhat closer to what you described, where simple casts between IFoo and IFoo2 no longer suffice.&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7741074</link><pubDate>Sun, 17 Feb 2008 00:09:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7741074</guid><dc:creator>Jack Mathews</dc:creator><description>&lt;p&gt;By the way, I'm not trying to be a jerk here. &amp;nbsp;I just don't want people to get the wrong idea about how C++ casts work and how objects are laid out.&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7743492</link><pubDate>Sun, 17 Feb 2008 03:32:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7743492</guid><dc:creator>LarryOsterman</dc:creator><description>&lt;p&gt;Jack, you're not. &amp;nbsp;But what happens if the class derives from IFoo and IFoo2 (I believe that it's legal).&lt;/p&gt;
&lt;p&gt;In that case, you have a case of multiple inheritance.&lt;/p&gt;
</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7754857</link><pubDate>Sun, 17 Feb 2008 19:31:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7754857</guid><dc:creator>Tim Smith</dc:creator><description>&lt;p&gt;Jack, if you and the compiler both know the structure of the COM object, then why are you even bothering to use COM? &amp;nbsp;&lt;/p&gt;
&lt;p&gt;For the current (and past) implementations of vtables in MSVC, what you say is correct. &amp;nbsp;However, what about Borland C++ or any other language that properly supports COM? &amp;nbsp;COM makes no statement as to the structure of the vtables for an object supporting multiple interfaces. It only makes a statement about each interface's vtable. &amp;nbsp;Since we are working with COM, we &amp;nbsp;only have available the rules COM provides us.&lt;/p&gt;
&lt;p&gt;Your statements, while correct depend on both the implementation of the object and the compiler in order to prove something true. &amp;nbsp;This is very dangerous in programming. &amp;nbsp;In Larry's case, he is using the very same details (with a easily corrected technical error) to show why something is bad. &amp;nbsp;That is perfectly legit.&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7754879</link><pubDate>Sun, 17 Feb 2008 19:33:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7754879</guid><dc:creator>Tim Smith</dc:creator><description>&lt;p&gt;BTW&lt;/p&gt;
&lt;p&gt;Jack, YOU are giving people the wrong idea about how C++ casts work and how objects are laid out. &amp;nbsp;They are very dangerous things to depend on.&lt;/p&gt;
&lt;p&gt;The things that you are saying are compiler implementation details and are NOT addressed in the C++ standard. &amp;nbsp;&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7767490</link><pubDate>Mon, 18 Feb 2008 08:54:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7767490</guid><dc:creator>Jack Mathews</dc:creator><description>&lt;p&gt;&amp;quot;Jack, you're not. &amp;nbsp;But what happens if the class derives from IFoo and IFoo2 (I believe that it's legal).&amp;quot;&lt;/p&gt;
&lt;p&gt;Yes, I brought that up in one of my messages.&lt;/p&gt;
&lt;p&gt;&amp;quot;Jack, if you and the compiler both know the structure of the COM object, then why are you even bothering to use COM? &amp;nbsp;&amp;quot;&lt;/p&gt;
&lt;p&gt;I'm only speaking to the example provided, not to COM in general.&lt;/p&gt;
&lt;p&gt;&amp;quot;COM makes no statement as to the structure of the vtables for an object supporting multiple interfaces.&amp;quot;&lt;/p&gt;
&lt;p&gt;Yes, but this Larry's friend's group does. &amp;nbsp;I'm only speaking to the example, where the people authoring the component set out specific guidelines. &amp;nbsp;Mind you, I don't agree with them either as it violates what COM is designed for, but that doesn't justify confusing people about how vc++ generates a vtable.&lt;/p&gt;
&lt;p&gt;In fact, that's really what I'm trying to point out here, that vtables are not generated this way for this single inheritance case in VC++ (or really any compiler).&lt;/p&gt;
&lt;p&gt;&amp;quot;Jack, YOU are giving people the wrong idea about how C++ casts work and how objects are laid out. &amp;nbsp;They are very dangerous things to depend on.&lt;/p&gt;
&lt;p&gt;Am I?&lt;/p&gt;
&lt;p&gt;For one, Larry's post spoke to an implementation defined way of vtable generation, and I corrected it. &amp;nbsp;The source material had nothing to do with the standard, so neither does my rebuttal.&lt;/p&gt;
&lt;p&gt;for another, my casting rules are absolutely correct. &amp;nbsp;Given a defined class hierarchy, you can cast from any point to any other point unless more than one copy of the class is in the hierarchy. &amp;nbsp;If you cast to a derived class knowing full well that you ARE that derived class, the cast will give you a valid pointer.&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7769128</link><pubDate>Mon, 18 Feb 2008 10:38:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7769128</guid><dc:creator>LarryOsterman</dc:creator><description>&lt;p&gt;To be fair: As far as I know, given what I stated in the post, Jack is totally right. &amp;nbsp;On Monday, I'm going to talk a bit about how the standard requires that a compiler handle multiple inheritance (it turns out that much of this vtable behavior is actually required by the standard). &amp;nbsp;As I said however, if the implementor of the class had chosen to implement both IFoo and IFoo2, my co-worker would have been up a creek - that's way too fragile to trust.&lt;/p&gt;
</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7783130</link><pubDate>Tue, 19 Feb 2008 02:53:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7783130</guid><dc:creator>Ulric</dc:creator><description>&lt;p&gt;I'm with Jack Mathews&lt;/p&gt;
&lt;p&gt;My first reaction to the blog was &amp;quot;what?&amp;quot; This up-cast to IFoo2 works perfectly in this simple example, and there is no reason why it could crash. &amp;nbsp;C++ insures this.&lt;/p&gt;
&lt;p&gt;So this is here entirely a question of style. You shouldn't mix C++ casting with COM, trying to go around QueryInterface. &amp;nbsp;The programmers I've seen doing this were usually driven by misguided beliefs about performance -- which happens a lot.&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7789387</link><pubDate>Tue, 19 Feb 2008 12:19:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7789387</guid><dc:creator>meh</dc:creator><description>&lt;p&gt;At the very beginning of the post it says &amp;quot;Someone had defined two COM interfaces&amp;quot;. COM, not C++.&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7805502</link><pubDate>Wed, 20 Feb 2008 02:44:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7805502</guid><dc:creator>Bob</dc:creator><description>&lt;p&gt;Just because you can do something doesn't mean you should.&lt;/p&gt;
&lt;p&gt;Even if God Almighty tells you the layout of the vtables, doing the cast in a way that depends on that detail creates coupling that is entirely unnecessary.&lt;/p&gt;
&lt;p&gt;C++ can ensure anything it wants - but what it can not ensure is that the IBar implementor will never change the way that the interfaces are being implemented. Even if there's only one person who owns all the relevant code, then you still need to remember (or leave a comment) explaining that the IBar implementation can't ever change because it would break a client module. I'm at a loss to understand why people consider that to be a good thing.&lt;/p&gt;
&lt;p&gt;I don't care how much you know about the compiler internals - I don't ever want to work with some of the people posting here (like Jack and Ulric) and would have no choice but to resort to physical violence if anyone tried to mutilate my code with such unnecessary assumptions about compiler implementation.&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7815098</link><pubDate>Wed, 20 Feb 2008 13:32:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7815098</guid><dc:creator>Sys64738</dc:creator><description>&lt;p&gt;Hi Larry,&lt;/p&gt;
&lt;p&gt;could you please - in other(s) blog post(s) - write about coding conventions you use in Microsoft?&lt;/p&gt;
&lt;p&gt;I'm very interested, and you have a good teaching style!&lt;/p&gt;
&lt;p&gt;About prefixing classes with C or interfaces with I... what's wrong with that? So you can read a name and immediately know that it is a class (C) or an interface (I). What is the alternative: Foo (class) and FooInterface (interface) ?&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;G&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7819185</link><pubDate>Wed, 20 Feb 2008 18:12:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7819185</guid><dc:creator>LarryOsterman</dc:creator><description>&lt;p&gt;Sys64738: &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/larryosterman/archive/2004/11/30/272437.aspx"&gt;http://blogs.msdn.com/larryosterman/archive/2004/11/30/272437.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Search for &amp;quot;Larry Osterman&amp;quot; &amp;quot;What Does Style Look Like&amp;quot; and you'll find a series of posts I did a few years ago on coding styles.&lt;/p&gt;
&lt;p&gt;There is no &amp;quot;microsoft&amp;quot; coding style - each team has its own style (sometimes each developer on the team has their own style).&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7833790</link><pubDate>Thu, 21 Feb 2008 12:34:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7833790</guid><dc:creator>Sys64738</dc:creator><description>&lt;p&gt;Thank you! This is what I looked for.&lt;/p&gt;
</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7835289</link><pubDate>Thu, 21 Feb 2008 14:08:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7835289</guid><dc:creator>Sys64738</dc:creator><description>&lt;p&gt;Wow! Your &amp;quot;What Does Style Look Like&amp;quot; series is *great*.&lt;/p&gt;
&lt;p&gt;Also some comments are very rich of insight!&lt;/p&gt;
&lt;p&gt;(BTW: I was also happy to read that you don't like C++ templates and operator overloading very much; I have the same opinion, I believe that C++ templates must be used &amp;lt;i&amp;gt;cum grano salis&amp;lt;/i&amp;gt; [latin].)&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;p&gt;G&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7852792</link><pubDate>Sat, 23 Feb 2008 04:44:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7852792</guid><dc:creator>Igor Levicki</dc:creator><description>&lt;p&gt;No wonder why today's code is so awfull with all those interfaces, factories and so many unknowns.&lt;/p&gt;
</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7887002</link><pubDate>Mon, 25 Feb 2008 05:56:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7887002</guid><dc:creator>Igor Levicki</dc:creator><description>&lt;p&gt;Larry, about your coding style, I disagree on two things:&lt;/p&gt;
&lt;p&gt;1. TABs are not hard, and SPACEs are not soft. It is vice versa.&lt;/p&gt;
&lt;p&gt;Rationale:&lt;/p&gt;
&lt;p&gt;In any decent editor TAB can be configured to an arbitrary number of SPACEs. However, arbitrary number of SPACEs cannot be converted automatically to the proper number of TABs in most editors. Therefore, by using SPACEs you are actually hard-coding indentation.&lt;/p&gt;
&lt;p&gt;2. if/else and braces&lt;/p&gt;
&lt;p&gt;Most developers write functions like this:&lt;/p&gt;
&lt;p&gt;void func(void)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	// code goes here&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;if you write if like this:&lt;/p&gt;
&lt;p&gt;if (condition)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	// do something&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;It resembles function which is confusing. Some people even dare to leave out the space between &amp;quot;if&amp;quot; and &amp;quot;(&amp;quot; (I really hate that).&lt;/p&gt;
&lt;p&gt;Rationale:&lt;/p&gt;
&lt;p&gt;if and else are language keywords and not functions. Functions cannot be nested in C. Do not write them such that they resemble functions. Write like this:&lt;/p&gt;
&lt;p&gt;if (condition) {&lt;/p&gt;
&lt;p&gt;	// do something&lt;/p&gt;
&lt;p&gt;} else {&lt;/p&gt;
&lt;p&gt;	// do something else&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Of course, I am not trying to force you to change your style, just to consider another point of view.&lt;/p&gt;
&lt;p&gt;My style is based upon Linux Kernel coding style and when I get unreadable code from someone here is what I do with it:&lt;/p&gt;
&lt;p&gt;indent.exe -kr -i8 -ts8 -sob -l80 -ss -ncs %1&lt;/p&gt;
&lt;p&gt;(hint: there is indent.exe for Windows too)&lt;/p&gt;
</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7888862</link><pubDate>Mon, 25 Feb 2008 08:52:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7888862</guid><dc:creator>LarryOsterman</dc:creator><description>&lt;p&gt;Igor, that's exactly why you use spaces, not tabs - because it hard codes indentation.&lt;/p&gt;
&lt;p&gt;Your alternative is that every developer has to have their editors configured identically. &amp;nbsp;And not every editor can be easily configured to always insert tabs. &amp;nbsp;If you use spaces, your indentation is always consistent, regardless of the settings of your editor.&lt;/p&gt;
&lt;p&gt;You're right that the if (x) { doesn't resemble function entries. &amp;nbsp;But it DOES make it more obvious when you forget an opening brace (and the style requires that there always be an opening brace).&lt;/p&gt;
&lt;p&gt;And I &amp;lt;i&amp;gt;NEVER&amp;lt;/i&amp;gt; gratuitously reformat someone else's code, regardless of how unreadable it is. &amp;nbsp;Instead, I change my style to match the coding style of the module in which it lives. &amp;nbsp;It's more important that the code in the module all look the same than that the code match your personal style.&lt;/p&gt;
</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7895272</link><pubDate>Mon, 25 Feb 2008 21:52:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7895272</guid><dc:creator>Igor Levicki</dc:creator><description>&lt;p&gt;Larry,&lt;/p&gt;
&lt;p&gt;Two things:&lt;/p&gt;
&lt;p&gt;1. I can't recall an editor (save notepad perhaps) where you can't set TAB size.&lt;/p&gt;
&lt;p&gt;2. If you accidentally delete a space somewhere and do not notice it immediately the indentation gets screwed later.&lt;/p&gt;
&lt;p&gt;3. TAB is consistent -- it is 8 characters by default. Linux Kernel Coding style warns against more than 3 levels of indentation and I think that warning has solid base. Code can always be written without deep nesting. Take a look at this example:&lt;/p&gt;
&lt;p&gt;	if (condition) {&lt;/p&gt;
&lt;p&gt;		if (condition2) {&lt;/p&gt;
&lt;p&gt;			if (condition3) {&lt;/p&gt;
&lt;p&gt;				// do something here&lt;/p&gt;
&lt;p&gt;			}&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;It can be written like this:&lt;/p&gt;
&lt;p&gt;	if (!condition) {&lt;/p&gt;
&lt;p&gt;		// goto or return&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;	if (!condition2) {&lt;/p&gt;
&lt;p&gt;		// goto or return&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;	if (!condition3) {&lt;/p&gt;
&lt;p&gt;		// goto or return&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;	// do something here&lt;/p&gt;
&lt;p&gt;In my opinion that is easier to follow than nested code.&lt;/p&gt;
&lt;p&gt;I reformat code given to me which no one else has to see again. I do not reformat for others, in that case I reformat for myself, edit and merge.&lt;/p&gt;
</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7896787</link><pubDate>Tue, 26 Feb 2008 01:26:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7896787</guid><dc:creator>Ulric</dc:creator><description>&lt;p&gt;Bob,&lt;/p&gt;
&lt;p&gt;&amp;gt;I don't care how much you know about the compiler internals - &lt;/p&gt;
&lt;p&gt;&amp;gt;I don't ever want to work with some of the people posting &lt;/p&gt;
&lt;p&gt;&amp;gt;here (like Jack and Ulric) and would have no choice but to &lt;/p&gt;
&lt;p&gt;&amp;gt;resort to physical violence&lt;/p&gt;
&lt;p&gt;Bob, it has nothing to do with hackery and relying or relying on shaky compiler internals. &amp;nbsp;It has to do with logic. Computer Science is about facts and logic, not about fear and belief. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;You could be the kind of person that would pepper the code base with &amp;quot;casts&amp;quot; because your 'not sure' what the compiler would do. &amp;nbsp;And you'd be hiding bugs and causing headaches for the team. &amp;nbsp;Some people don't understand what are the facts and rules and see it as all magic and chance, and copy/paste recipes. &amp;nbsp;What if the compile didn't follow the C++ standard, heh? &amp;nbsp;Where do you draw the line at what's 'internal'?&lt;/p&gt;
&lt;p&gt;I had a work term student tell me once that he wasn't sure that items in an array, or rows of arrays, were contiguous in memory. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I write, port and refact, c++ code between VC++, gcc on linux. &amp;nbsp;It's the largest application you can image (300 dlls on windows) &amp;nbsp;I'm pretty clear about portable, safe and correct code.&lt;/p&gt;
&lt;p&gt;From my side, reading &amp;quot;if he was lucky, his code would crash&amp;quot; pushed my &amp;quot;failed logic&amp;quot; button... &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt;The behavior is not uncertain or random...&lt;/p&gt;
&lt;p&gt;It's a style issue. I am not recommending doing this at all and would never permit it. &amp;nbsp; &amp;nbsp;The way I understood the article, it was arguing that it wasn't strictly a style issue. &amp;nbsp;But inside the parameters of the question, it's all about style. &amp;nbsp;Don't fight the COM, man. &amp;nbsp;I don't think anyone here suggests that.&lt;/p&gt;
&lt;p&gt;The more you do refactoring, you get to ask question that others may feel you shouldn't ask. It's always very enjoyable to find the solutions and the explanations.&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7911727</link><pubDate>Wed, 27 Feb 2008 03:13:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7911727</guid><dc:creator>Norman Diamond</dc:creator><description>&lt;p&gt;&amp;gt; It can be written like this:&lt;/p&gt;
&lt;p&gt;&amp;gt; if (!condition) {&lt;/p&gt;
&lt;p&gt;&amp;gt; // goto or return&lt;/p&gt;
&lt;p&gt;&amp;gt; }&lt;/p&gt;
&lt;p&gt;&amp;gt; if (!condition2) {&lt;/p&gt;
&lt;p&gt;&amp;gt; // goto or return&lt;/p&gt;
&lt;p&gt;&amp;gt; }&lt;/p&gt;
&lt;p&gt;&amp;gt; if (!condition3) {&lt;/p&gt;
&lt;p&gt;&amp;gt; // goto or return&lt;/p&gt;
&lt;p&gt;&amp;gt; }&lt;/p&gt;
&lt;p&gt;&amp;gt; // do something here&lt;/p&gt;
&lt;p&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;gt; In my opinion that is easier to follow than nested code.&lt;/p&gt;
&lt;p&gt;In other words: &amp;nbsp;absence-of-goto considered harmful.&lt;/p&gt;
&lt;p&gt;&amp;lt;temporary digression into facts&amp;gt;&lt;/p&gt;
&lt;p&gt;Code indented by spaces can be a nuisance to edit.&lt;/p&gt;
&lt;p&gt;Code indented by tabs can be a nuisance to edit.&lt;/p&gt;
&lt;p&gt;&amp;lt;/temporary digression into facts&amp;gt;&lt;/p&gt;
&lt;p&gt;Therefore we should do what VC++ 2005 does, insert spaces sometimes and insert tabs sometimes. &amp;nbsp;Or do what vim does, insert tabs even if the preceding line had spaces.&lt;/p&gt;
&lt;p&gt;&amp;gt; I do not reformat for others, in that case I reformat for myself, edit and merge.&lt;/p&gt;
&lt;p&gt;When you're done, do you reformat back to the original coder's style?&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7911764</link><pubDate>Wed, 27 Feb 2008 03:17:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7911764</guid><dc:creator>Norman Diamond</dc:creator><description>&lt;p&gt;&amp;quot;I had a work term student tell me once that he wasn't sure that items in an array, or rows of arrays, were contiguous in memory.&amp;quot;&lt;/p&gt;
&lt;p&gt;Unfortunately your student has powerful forces on her/his side. &amp;nbsp;For 20 years the C and C++ standards have been working very hard to cloud up this particular issue.&lt;/p&gt;
&lt;p&gt;(Or if your student meant padding to align each item or row then your student was right all along. &amp;nbsp;But I think you didn't mean that because it would be too obvious ^_^)&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7915000</link><pubDate>Wed, 27 Feb 2008 09:13:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7915000</guid><dc:creator>*null</dc:creator><description>&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.sellsbrothers.com/fun/#re_re_Do_Not_Mix_Cpp_Cast_and_COM"&gt;http://www.sellsbrothers.com/fun/#re_re_Do_Not_Mix_Cpp_Cast_and_COM&lt;/a&gt;&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7928153</link><pubDate>Thu, 28 Feb 2008 06:30:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7928153</guid><dc:creator>Igor Levicki</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt;Code indented by spaces can be a nuisance to edit.&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;This is true, there are still some editors which do not understand the concept that n spaces = 1 tab. Therefore you have to delete and add spaces one by one.&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;Code indented by tabs can be a nuisance to edit.&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;Can you give an example?&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt;When you're done, do you reformat back to the original coder's style?&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;I said I merge -- that means I just apply my coding changes to the original source file without applying my style.&lt;/p&gt;
</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7928308</link><pubDate>Thu, 28 Feb 2008 06:42:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7928308</guid><dc:creator>Igor Levicki</dc:creator><description>&lt;p&gt;Oh there is one more thing which is part of the coding style but people don't discuss it often -- functions which return BOOL. They write such a function like this:&lt;/p&gt;
&lt;p&gt;BOOL IsReadable(BYTE *mem)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	// test if readable&lt;/p&gt;
&lt;p&gt;	return TRUE;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;And then they call it like this:&lt;/p&gt;
&lt;p&gt;if (!IsReadable(mem)) {&lt;/p&gt;
&lt;p&gt;	// goto or return&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Then in order to avoid writing !IsReadable() which is awkward the write nested ifs:&lt;/p&gt;
&lt;p&gt;if (IsReadable(mem)) {&lt;/p&gt;
&lt;p&gt;	if (condition2) {&lt;/p&gt;
&lt;p&gt;		if (condition3) {&lt;/p&gt;
&lt;p&gt;			// do something&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Instead of doing it like this:&lt;/p&gt;
&lt;p&gt;BOOL NotReadable(BYTE *mem)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	// test if NOT readable&lt;/p&gt;
&lt;p&gt;	return TRUE;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;And calling it like this:&lt;/p&gt;
&lt;p&gt;if (NotReadable(mem)) {&lt;/p&gt;
&lt;p&gt;	// goto or return&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;It avoids double negations, nesting ifs, and it is much easier to read.&lt;/p&gt;
</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#7930382</link><pubDate>Thu, 28 Feb 2008 10:04:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7930382</guid><dc:creator>Norman Diamond</dc:creator><description>&lt;p&gt;&amp;quot;Code indented by tabs can be a nuisance to edit.&amp;quot;&lt;/p&gt;
&lt;p&gt;'Can you give an example?'&lt;/p&gt;
&lt;p&gt;Probably not the kind of example you want.&lt;/p&gt;
&lt;p&gt;The usual case is a VC++ source file because Visual Studio usually inserts spaces to indent lines in other languages. &amp;nbsp;Sometimes I use vim instead of Visual Studio to edit a single source file. &amp;nbsp;Sometimes I use vim at the same time as Visual Studio because I can do some repetitive edit faster in vim than in Visual Studio (maybe less than others could do with emacs or with Visual Studio macros or add-ins, but still sometimes the tradeoff tilts me towards vim). &amp;nbsp;If parts of an expression or function header or whatever were lined up in Visual Studio then they're not lined up any more in vim.&lt;/p&gt;
&lt;p&gt;A less common case is C# or VB, for exactly the opposite reason. &amp;nbsp;vim likes to indent using tabs even if the adjacent lines have spaces. &amp;nbsp;Then the results come out wrong in Visual Studio.&lt;/p&gt;
&lt;p&gt;Now, one might think that if one only uses Visual Studio then this problem won't occur. &amp;nbsp;But it still did, I have a VC++ source file that has some lines indented using spaces and some indented using tabs. &amp;nbsp;I don't know how it happened in this one.&lt;/p&gt;
&lt;p&gt;For one project I used Wordpad to edit .rc files in a Unicode encoding, I think UTF-16.&lt;/p&gt;</description></item><item><title>re: Casting from one interface to another...</title><link>http://blogs.msdn.com/larryosterman/archive/2008/02/15/casting-from-one-interface-to-another.aspx#8373843</link><pubDate>Thu, 10 Apr 2008 05:25:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8373843</guid><dc:creator>chrisg</dc:creator><description>&lt;p&gt;this problem would be avoided if IBar::GetFoo() used the REFIID/void** pattern to return the object that implements IFoo or IFoo2 (or IFoo3 for that matter).&lt;/p&gt;
&lt;p&gt;this of course has a downside, you can't read the method signature and know what types to expect. &lt;/p&gt;
&lt;p&gt;to mitigate that put a comment above the method that lists the interfaces you can expect to request.&lt;/p&gt;</description></item></channel></rss>