Visual C++ Class Designer

Back in 2005, when covering the upcoming release of Visual C++ 2005, the use of the Class Designer to document and create C++ code was discussed, but unfortunately this functionality was pulled in the Beta 2 release of Visual C++ 2005. With Visual C++ 2008, support for C++ in the Class Designer is back, but with a few limitations. Getting the bad news out of the way first, the biggest surprise is that managed C++ classes are not supported by the Class Designer, and only native types can be displayed. The second major limitation is that the Class Designer has no support for modifying the displayed types using the Class Designer surface, unlike C# and Visual Basic.NET which support a bi-directional design experience where modifications in the Class Designer are reflected in code.

With these major limitations out of the way, it's worth looking at how much the Class Designer can actually do for C++ developers. The range of native C++ constructs that the Class Designer can display is quite impressive. Starting with a simple example, consider the following native C++ type definition:

class NativeClass
{
public:
   NativeClass(void) {};
   ~NativeClass(void) {};
   int PublicInt;
   virtual char* GetString(int number) {return NULL;}
   ///<summary>Always returns zero</summary>
   inline int GetInt() {return 0;}
private:
   double PrivateD;
};

To view this type in the Class Designer, simply select the header file in Solution Explorer, and click the view Class Diagram button, which is located in a toolbar at the top of the Solution Explorer window, as shown in Figure 1.

Figure 1: Basic Class Diagram View

Figure 1 shows the default display for a type on the Class Designer surface, and the Class Designer toolbar is also visible in Figure 1. As is apparent, the default view is fairly useless, and it will generally be necessary to change the display layout to get some useful information. By clicking on the small double-arrow head located near the name of the class (circled in Figure 2), detailed information on the member variables and methods of the class can be seen, as shown in Figure 2.

Figure 2: Class Diagram Detailed View

Figure 2 also shows the Properties window and Class Details windows displayed, and with this information, the visibility, return type, and summary of all methods and member variables can be seen. When a method is selected in the Class Details window, further information about whether the method is virtual or inline can be seen in the Properties window. The detailed class information in Figure 2 is achieved by selecting the Display Full Signature option from the Class Designer toolbar. The other two display options are Display Name, which only shows method and member variable names as the option suggest, and Display Name and Type, which shows all the information in Figure 2 with the exception of parameters to methods.

Visual C++ Class Designer

Visual C++ supports having multiple Class Diagrams in a project, with each Class Diagram having the file extension .cd, and maintaining all the layout options that have been selected when the diagram was created. To add a new diagram to a project, simply select a new header file containing a native class definition, and click the View Class Diagram button. To add other types to an existing diagram, the header file containing the type definition needs to be dragged onto the Class Designer design surface. Doing this will add all the types declared in the header files onto the design surface, and those types that are not required can be selected and deleted.

Apart from diagramming an existing application, one of the most useful features of the Class Designer is its ability to navigate around the methods and member variables in a complex code base. Whereas most code bases will follow some convention about how to structure a class across a header and implementation file, code bases that have been around for a long time can suffer from developers who seem intent on hiding the implementation for a method as far away from its header file as possible. By right-clicking on any method or type in the Class Designer and selecting View Code from the context menu, the header or implementation file that contains the implementation will be navigated to, regardless where it is located inside the project.

As would be expected on any worthwhile native C++ code diagramming tool, the display of multiple inheritance is supported, and Figure 3 shows a type called Derived that has a public derivation from NativeClass and a private derivation from Base, as shown by the arrows. The base classes of a type are also displayed within the derived types details, which allows derived types to be identified even if their base types are not present on the diagram.

Figure 3: Multiple Inheritance View

In addition to the basic C++ native classes that have been shown so far, the Class Designer supports the display of template classes, structs, enums, and macros. Of these, the most interesting are templates, given the complexity in type construction that they support. The Class Designer can show only types defined in header files that are part of a C++ project, so if a Standard C++ library type needs to be displayed, the header file that defines the type must be included in the project. By adding the STL header file that defines the vector container in the project and dragging the header file into a Class Designer, the output shown in Figure 4 was generated. As can be seen, despite the complexity of the STL template, the display of information produced by the Class Designer is clear and meaningful.

Figure 4: Complex Template Class Display

The Class Designer allows a diagram to be exported to a variety of different image file formats, and this functionality was used to produce the diagram for Figure 4.

In addition to the two major limitations covered at the beginning of the article, there are a few other much smaller limitations worth covering:

  • Type definitions must be in a header file.
  • Global functions cannot be displayed.
  • Unions cannot be displayed.
  • Primitive types such as integers and floating point number cannot be displayed as individual types (but will be displayed when they are member variable or method parameters).
  • COM types defined in IDL files cannot be displayed.

The MSDN documentation for Beta 2 of Visual Studio 2008 states that nested types cannot be displayed by the Class Designer, but as Figure 4 shows, this is clearly not the case.

Conslusion

Although the lack of support for managed type display in the Class Designer is in some ways surprising and a little disappointing, it is certainly in line with the new Visual C++ philosophy that focuses on native and interoperability development rather than managed development. The Visual C++ 2008 Class Designer is certainly a respectable offering for an initial release, and confirms Microsoft's continued commitment to Visual C++ as a product. For Visual C++ developers who haven't settled on a third-party UML tool for documenting their applications, the Class Designer will be a welcome addition to C++, and is certainly worth trying out.

About the Author

Nick Wienholt is an independent Windows and .NET consultant based in Sydney, Australia. He is the author of Maximizing .NET Performance from Apress, and specialises in system-level software architecture and development with a particular focus on performance, security, interoperability, and debugging. Nick can be reached at NickW@dotnetperformance.com.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds