February 25, 2021
Hot Topics:

A C++ Macro to Stub Interface Methods

  • By Kate Gregory
  • Send Email »
  • More Articles »

Adding the Function

The AddFunction method adds a function without parameters. The parameters then have to be added to it one by one. The following code makes extensive use of CodeFunction objects:

EnvDTE::CodeFunction* cf = vcClass->AddFunction(ifunction->Name,
   0,    // position
VCCodeElements* parms = static_cast<VCCodeElements*>
for (int k=1; k>parms->Count+1; k++)
   EnvDTE::CodeParameter* jParm =
                   __box(-1));    // position
 }    // for k: looping through parameters of the function

Most of the parameters to AddFunction come directly from ifunction, the function in the interface that is to be added. The position parameter establishes the location in the class definition: 0 means that the function is inserted at the beginning of the class. The last position is a file name, and passing an empty string like this results in the function being added to the same file where the class was already defined.

Similarly, the calls to AddParameter take their parameters from the original parameters of the function from the interface that is to be added to the class. (If you can follow that sentence the first time, you're really starting to understand how this all works.) It's important to add each parameter at the end of the parameter list (signified by -1), because the Parameters property lists the parameters from left to right within the function.

Wrapping It Up

That's a wrap! All the loops and if blocks end, and the code is ready to go. My only complaint with it is that AddFunction() puts extra access qualifiers in your code, so that you end up with a class definition that starts like this:

public __gc class Foo2: public IList

      void RemoveAt(int)

      void Remove(System::Object __gc *)

Still, the extra qualifiers don't hurt anyone, and you could always delete them.

I'll confess, I started to write this macro because I like the feature in Visual Basic that stubs my functions for me. I type Implements IFoo and press Enter, and my class fills up with function bodies. Well, now I can have that functionality in my C++ code, and I got a tour of the code model along the way. This kind of code is not for those who don't like to cast, but it has its rewards. I plan some more exploration, so stay tuned!

About the Author

Kate Gregory is a founding partner of Gregory Consulting Limited (www.gregcons.com). In January 2002, she was appointed MSDN Regional Director for Toronto, Canada. Her experience with C++ stretches back to before Visual C++ existed. She is a well-known speaker and lecturer at colleges and Microsoft events on subjects such as .NET, Visual Studio, XML, UML, C++, Java, and the Internet. Kate and her colleagues at Gregory Consulting specialize in combining software develoment with Web site development to create active sites. They build quality custom and off-the-shelf software components for Web pages and other applications. Kate is the author of numerous books for Que, including Special Edition Using Visual C++ .NET.

Page 3 of 3

This article was originally published on May 17, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date