I have been using Eclipse CDT for some time now, and I love it, but there are a few tedious things that I would like to fix up about it.
When you create a new file, one of the options is "New"->"Class". I was wondering if anyone knows of a way to edit the "${declarations}" section of this "Class" template.
To be more specific, I have looked through the "Window"->"Preferences" menu and been unable to find anything. I have changed both "C/C++"->"Code Style"->"Code Templates" and "C/C++"->"Editor"->"Templates". Only the first of the two actually seems to change what appears upon class creation, and it doesn't let me change what is in the "${declarations}" section. Does anyone know how to change this?
Thanks, Chris
The preference C/C++ -> Editor -> Templates
is used by the templates which are inserted manually via Context Assist. Try create a new file, type clas
and press ctrl+space
for context assist. You should get two assist proposals: a keyword proposal and a template proposal (the latter will also be triggered automatically as default if you type class
and press ctrl+space
).
Upon selection of the template proposal, a class body will be generated according to the template which you can define in this preference.
As for C/C++ -> Code Style -> Code Templates
, this is used in automatic generation. When you use the New Class wizard, the Default C++ Source template
and Default C++ Header template
are used and the $(declarations)
variable is replaced by whatever code is generated by the New Class wizard.
This would mean that you can use a custom template by triggering one of the Editor templates manually, possibly via creating a named class with New Class wizard and then replacing the default class body in header by your custom template.
Or do you suggest that the New Class wizard lacks any important fields and should be extended?
Or do you suggest that the New Class wizard lacks any important fields and should be extended?
Yes private copy and assign operator.
Oooh! Finally found an answer.
File -> Preferences
. C/C++ -> Code Style -> Code Templates
.Code Templates
window, navigate to Files -> C++ Header File
.${declarations}
line, and insert your own stuff. This has the somewhat unfortunate disadvantage of losing everything that the New Class Wizard would produce. This might invalidate any inherited classes or other things you might select from the New Class Wizard, but I haven't checked.
Here's my C++ Header File Template:
${filecomment}
#ifndef ${include_guard_symbol}
#define ${include_guard_symbol}
${includes}
${namespace_begin}
class ${type_name}
{
public:
/* Default Constructor */
${type_name}();
/* Deconstructor */
virtual ~${type_name}();
private:
/* Disable the following by default */
${type_name}(const ${type_name}& other);
${type_name}& operator=(const ${type_name}&);
};
${namespace_end}
#endif /* ifndef ${include_guard_symbol} */
Hope this helps!
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