Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse CDT "New Class" Template

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

like image 581
Chris Covert Avatar asked Jul 19 '10 21:07

Chris Covert


3 Answers

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?

like image 186
Kos Avatar answered Dec 09 '22 21:12

Kos


Or do you suggest that the New Class wizard lacks any important fields and should be extended?

Yes private copy and assign operator.

like image 21
Stefan Bellus Avatar answered Dec 09 '22 21:12

Stefan Bellus


Oooh! Finally found an answer.

  1. In Eclipse CDT (Juno), go to File -> Preferences.
  2. Navigate to the following pane on the left panel of the Preferences window: C/C++ -> Code Style -> Code Templates.
  3. In the Code Templates window, navigate to Files -> C++ Header File.
  4. Edit the header file:
  5. Remove the ${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!

like image 31
jvriesem Avatar answered Dec 09 '22 23:12

jvriesem