brainydexter
brainydexter

Reputation: 20356

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 ?

Upvotes: 11

Views: 4968

Answers (4)

shycha
shycha

Reputation: 462

Checked in MVS 2008...

File: C:\Program Files\Microsoft Visual Studio 9.0\VC\VCWizards\CodeWiz\Generic\Class\Scripts\1033\default.js

Added code (after creating default ctor & dtor in the default.js)

    var oCopyCtor = newclass.AddFunction(strClassName+"(const "+strClassName+"& refObj)", vsCMFunctionConstructor, "", vsCMAddPositionEnd, vsCMAccessPrivate, strImpl);
    var oAssignmentOperator = newclass.AddFunction("operator=(const "+strClassName+"& rhs)", vsCMFunctionOperator, strClassName+"&", vsCMAddPositionEnd, vsCMAccessPrivate, strImpl);

    oAssignmentOperator.BodyText = "if(&rhs == this) { return *this; }\n//TODO: real assignment here...\nreturn *this;\n"

But I still can't figure out how to turn off implementation in *.cpp (x.BodyText = ""; doesn't help), and omitting strImpl parameter puts the implementation in the *.h file

Upvotes: 0

User
User

Reputation: 65961

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

Upvotes: 3

Max
Max

Reputation: 3180

(If I understand correctly)

I don't think you can modify the code that is auto-generated by a wizard, for example when adding a new class.

You could maybe code a new wizard ? M.

Upvotes: 0

Roger Lipscombe
Roger Lipscombe

Reputation: 91855

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.

Upvotes: 1

Related Questions