CLion 2018.2 Help

Generating Code

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.
  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:

public class MyClass { int aInteger; double bDouble; public MyClass(int myAIntegerParam, double myBDoubleParam) { aInteger = myAIntegerParam; bDouble = myBDoubleParam; } }

Generate delegation methods

CLion can generate methods that delegate behavior to the fields or methods of your class. This approach makes it possible to give access to the data of a field or method without directly exposing this field or method.

To generate a delegation method for a class:

  1. On the Code menu, click Generate.
  2. In the Generate popup, click Delegate Methods.
  3. Select the target field or method, and click OK.
  4. Select the desired methods to be delegated and click OK.

The following code fragment shows the result of delegating the get(i) method of the Calendar class inside another class:

Calendar calendar; public int get(int i) { return calendar.get(i); }

Generate getters and setters

CLion can generate accessor and mutator methods (getters and setters) for the fields in your classes. Generated methods have only one argument.

To generate getters and setters for a class:

  1. On the Code menu, click Generate.
  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.

The following code fragment shows the result of generating the getter and setter methods for a class with one field var:

public class MyClass { int field; public int getField() { return field; } public void setField(int field) { this.field = field; } }
Last modified: 18 September 2018