Macro to Add New Classes to Visual C++ Projects

I use the following macro to add a class to my project. There are other implementations of this sort of macro.
However, I believe that my version does a little bit more than most and even it adds some basic functions to
the newly created class. As an example, this macro will not only create the class’ definition for you, it also
automatically generates such basic class functions as the constructor, destructor and copy constructor.

In addition, I always use a protected assign method; I call this method both from my operator= as
well as my copy constructor. I put the assign function in the code too. Obviously not everyone has
the same function requirements so feel free to edit away!


Function GetProjectPath(ByVal proj)
dim szProjectPath
dim nIndex
dim szReturn

szProjectPath = proj.FullName
nIndex = InStrRev(szProjectPath, “”)

if (nIndex <> 0) then
szReturn = Left(szProjectPath, nIndex)
end if

GetProjectPath = szReturn
End Function

Sub ClassGenerator
‘DESCRIPTION: Creates minimal source/header files for a
‘ class, given its input name.
On Error Resume Next

dim szClassName
dim szClassRoot ‘ classname minus any MFC-style prefix
dim szSourceFile
dim szHeaderFile

‘ used to determine full paths for the added
‘ source/header file
dim szProjectPath

dim szTempPath
dim szHeaderPreprocessor, szHeaderClassBegin
dim szFirstChar, szSecondChar

‘ make sure the project is valid
if ActiveProject.Type <> “Build” then
MsgBox “This project is not valid. Ending macro.”
Exit Sub
end if

‘ enter the class name
szClassName = InputBox(“Enter the class name:”,
“Class Name”)
if (len(szClassName) <= 0) then MsgBox "Invalid class name. Ending macro." Exit Sub end if ‘ generate file names based on the input
‘ class name
szSourceFile = szClassName + “.cpp”
szHeaderFile = szClassName + “.h”

‘ test for MFC style class names [ie: CClassName’s
‘ file should be ClassName.h”] all we do is check the
‘ first to characters; if they’re both capitals, we
‘ don’t use the first one
szFirstChar = Left(szClassName, 1)
szSecondChar = Mid(szClassName, 2, 1)

if ( ((Asc(szFirstChar) >= 65) and (Asc(szFirstChar) <= 90)) _ and ((Asc(szSecondChar) >= 65) and (Asc(szSecondChar) <= 90)) ) _ then szSourceFile = Mid(szSourceFile, 2) szHeaderFile = Mid(szHeaderFile, 2) szClassRoot = Mid(szClassName, 2) else szClassRoot = szClassName end if ‘ add the files to the project — if they can’t be added,
‘ Resume Next will see to it that we skip past this
ActiveProject.AddFile szSourceFile
ActiveProject.AddFile szHeaderFile

‘ get the project path
szProjectPath = GetProjectPath(ActiveProject)

‘ now add the header file to the hard drive
Documents.Add “Text”

ActiveDocument.Selection.StartOfDocument

ActiveDocument.Selection = “#ifndef ” & UCase(szClassRoot) _
& “_H_” & vbCrLf & _
“#define ” & UCase(szClassRoot) & “_H_” & vbCrLf & _
vbCrLf & _
“class ” & szClassName & vbCrLf & _
“{” & vbCrLf & _
“public: // object creation/destruction” & vbCrLf & _
” ” & szClassName & “();” & vbCrLf & _
” ” & szClassName & “(const ” & szClassName & “& source);” _
& vbCrLf & _
” ” & szClassName & “& ” & “operator=(const ” & szClassName _
& “& right);” & vbCrLf & _
” virtual ” & “~” & szClassName & “();” & vbCrLf &_
vbCrLf &_
“public: // attribute modification” & vbCrLf & _
vbCrLf & _
“protected: // protected members” & vbCrLf & _
” void assign(const ” & szClassName & “& source);” _
& vbCrLf & vbCrLf & _
“private: // attributes” & vbCrLf & _
“};” & vbCrLf & _
vbCrLf & _
“#endif”

ActiveDocument.Save szProjectPath & szHeaderFile

‘ now add the source file to the hard drive
Documents.Add “Text”

ActiveDocument.Selection.StartOfDocument

ActiveDocument.Selection = “#include ” & Chr(34) & szHeaderFile _
& Chr(34) & vbCrLf & vbCrLf & _
szClassName & “::” & szClassName & “()” & vbCrLf & _
“{” & vbCrLf & _
” // nothing to do yet” & vbCrLf & _
“}” & vbCrLf & _
vbCrLf & _
szClassName & “::” & szClassName & “(const ” & szClassName _
& “& source)” & vbCrLf & _
“{” & vbCrLf & _
” assign(source);” & vbCrLf & _
“}” & vbCrLf & _
vbCrLf & _
szClassName & “& ” & szClassName & “::” & “operator=(const ” _
& szClassName & “& right)” & vbCrLf & _
“{” & vbCrLf & _
” if (this != &right)” & vbCrLf & _
” {” & vbCrLf & _
” assign(right);” & vbCrLf & _
” }” & vbCrLf & _
vbCrLf & _
” return (*this);” & vbCrLf & _
“}” & vbCrLf & _
vbCrLf & _
szClassName & “::~” & szClassName & “()” & vbCrLf & _
“{” & vbCrLf & _
” // nothing to do yet” & vbCrLf & _
“}” & vbCrLf & _
vbCrLf & _
“void ” & szClassName & “::assign(const ” & szClassName _
& “& source)” & vbCrLf & _
“{” & vbCrLf & _
” // assign all of source’s members to this*” & vbCrLf & _
“}” & vbCrLf

ActiveDocument.Save szProjectPath & szSourceFile

End Sub

More by Author

Must Read