CLion 2019.1 Help

Code Generation

Generate constructors

CLion can generate a constructor that initializes specific class fields using values of corresponding arguments.

To generate a constructor for a class:

  1. On the Code menu, click Generate (Alt+Insert).

  2. In the Generate popup, click Constructor.

  3. If the class contains fields, select the fields to be initialized by the constructor and click OK.

The following code fragment shows the result of generating a constructor for a class:

Initial

Constructor generated in-place

class NewClass { protected: int a = 1; int b = 1; double c; };

class NewClass { public: NewClass(int aparam, int bparam, double cparam) :a(aparam), b(bparam), c(cparam) { } protected: int a = 1; int b = 1; double c; };

Generate getters and setters

CLion can generate accessor and mutator methods (getters and setters) for the fields in your classes..

Generate getters and setters for a class:

  1. On the Code menu, click Generate (Alt+Insert).

  2. In the Generate popup, click one of the following:

    • Getter to generate accessor methods for getting the current values of class fields.

    • Setter to generate mutator methods for setting the values of class fields.

    • Getter and Setter to generate both accessor and mutator methods.

  3. Select the fields to generate getters or setters for and click OK.

Initial

Setters generated for all fields

class NewClass { public: NewClass(int aparam, int bparam, double cparam) :a(aparam), b(bparam), c(cparam) { } protected: int a = 1; int b = 1; double c; };

class NewClass { public: NewClass(int aparam, int bparam, double cparam) :a(aparam), b(bparam), c(cparam) { } void setA(int aparam) { NewClass::a = aparam; } void setB(int bparam) { NewClass::b = bparam; } void setC(double cparam) { NewClass::c = cparam; } protected: int a = 1; int b = 1; double c; };

Last modified: 5 April 2019