Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override default c++ class template in visual studio 2010

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 ?

like image 308
brainydexter Avatar asked Feb 18 '11 16:02

brainydexter


People also ask

How do I change a class template in Visual Studio?

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.

How do I import a Visual Studio template?

Project -> Export Template… Select Project Template. Select source Project. In the Template Options I choose to Automatically Import the template to Visual Studio.

What is class template in C Plus Plus?

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.


2 Answers

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

like image 70
User Avatar answered Oct 12 '22 06:10

User


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.

like image 33
Roger Lipscombe Avatar answered Oct 12 '22 04:10

Roger Lipscombe