When I create a new C++ class in visual studio 2010, it generates a class with some template code. How can I modify this template to suit my own needs ?
Use the Export Template WizardChoose File > New > Project from the menu bar. Select the template that you want to update and continue through the steps to create the new project. Modify the project in Visual Studio. For example, change the output type or add a new file to the project.
Project -> Export Template… Select Project Template. Select source Project. In the Template Options I choose to Automatically Import the template to Visual Studio.
The relationship between a class template and an individual class is like the relationship between a class and an individual object. An individual class defines how a group of objects can be constructed, while a class template defines how a group of classes can be generated.
One problem with finding info about this is most of information about creating templates is for .NET and the process is different for Visual C++. Also the answer is probably not what you want to hear because it involves editing javascript code rather than just editing some template file. It's possible that you can create a brand new wizard that uses a template file, but this is one way to modify the default template without doing that. Modifying the wizard code involves editing a javascript file:
C:\Program Files\Microsoft Visual Studio 10.0\VC\VCWizards\CodeWiz\Generic\Class\Scripts\1033\default.js
The javascript uses the CodeModel to manipulate (or generate, in this case) source code. Inside that file there is an OnFinish
function which you can use to modify the class details that are output. You will see a line like this in the file:
var newclass = oCM.AddClass(strClassName,
strHeader, vsCMAddPositionEnd, "", "", vsCMAccessDefault);
to add a new function you would do something like:
newclass.AddFunction("MyFunction", vsCMFunctionFunction,
vsCMTypeRefVoid, vsCMAddPositionEnd, vsCMAccessPublic, strImpl);
You can read about it here:
Inside Visual C++ Wizards
Reference Documentation:
Designing a Wizard
The default templates are in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcprojectitems
. Change as appropriate for x86-vs-x64 and VS version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With