Code Examples
  Home arrow Code Examples arrow Page 5 - Style Case Studies: Construction Unions
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Style Case Studies: Construction Unions
By: Addison-Wesley/Prentice Hall PTR
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2004-11-16

    Table of Contents:
  • Style Case Studies: Construction Unions
  • Solution
  • Dissecting Construction Unions
  • Into the Code
  • Comments
  • Adding New Types
  • Underhanded Names
  • Toward a Better Way: boost::any
  • Alexandrescu’s Discriminated Unions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Style Case Studies: Construction Unions - Comments


    (Page 5 of 9 )

      union {
        int i;
        unsigned char buff[max(sizeof(LIST),sizeof(STRING))];
      } U;
      void cleanup();
    };
    That’s it for the main class definition. Moving on, consider the three parallel accessor functions:
    inline int& MYUNION::getint()
    {
      if( currtype==_INT ) {
        return U.i;
      } else {
        cleanup();
        currtype=_INT;
        return U.i;
      } // else
    }
    inline LIST& MYUNION::getlist()
    {
      if( currtype==_LIST ) {
        return *(reinterpret_cast<LIST*>(U.buff));
      } else {
        cleanup();
       
    LIST* ptype = new(U.buff) LIST();
        currtype=_LIST;
        return *ptype;
      } // else
    }
    inline STRING& MYUNION::getstring()
    {
      if( currtype==_STRING) {
        return *(reinterpret_cast<STRING*>(U.buff));
      } else {
        cleanup();
        STRING* ptype = new(U.buff) STRING();
        currtype=_STRING;
        return *ptype;
      } // else
    }


    A minor nit: The // else comments add nothing. It’s unfortunate that the only comments in the code are useless ones.
     
    Guideline: Write (only) useful comments. Never write comments that repeat the code; instead, write comments that explain the code and the reasons why you wrote it that way.

    More seriously, there are three major problems here. The first is that the functions are not written symmetrically, and whereas the first use of a list or a string yields a default-constructed object, the first use of int yields an uninitialized object. If that is intended, in order to mirror the ordinary semantics of uninitialized int variables, then that should be documented; because it is not, the int ought to be initialized. For example, if the caller accesses getint and tries to make a copy of the (uninitialized) value, the result is undefined behavior—not all platforms support copying arbitrary invalid int values, and some will reject the instruction at run-time.

    The second major problem is that this code hinders const-correct use. If the code is really going to be written this way, then at least it would be useful to also provide const overloads for each of these functions; each would naturally return the same thing as its non-const counterpart, but by a reference to const.

    Guideline: Practice const-correctness.

    The third major problem is that this approach is fragile and brittle in the face of change. It relies on type switching, and it’s easy to accidentally fail to keep all the functions in sync when you add or remove new types.

    Stop reading here and consider: What do you have to do in the published code if you want to add a new type? Make as complete a list as you can.

    This chapter is from Exceptional C++ Style, by Herb Sutter (ISBN 0201760428, copyright 2005. All rights reserved. It is reprinted with permission from Addison-Wesley Professional). Check it out at your favorite bookstore today.

    Buy this book now.

    More Code Examples Articles
    More By Addison-Wesley/Prentice Hall PTR


     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT