CLion 2022.2 Help

Pull members up, push members down

The Pull Members Up refactoring allows you to move class members to a base class.

The Push Members Down refactoring helps you clean up the class hierarchy by moving class members to a subclass. The members are then relocated into the direct subclasses only.

Pull members up

  1. Select the members to be moved to a superclass.

  2. From the main or context menu, choose Refactor | Pull Members Up. The Pull Members Up dialog appears.

  3. Select the destination object (superclass).

  4. In the Members section, select the members you want to move.

  5. Click Refactor to pull the selected members to their destination.

Examples

Before

After

class BaseClass { int d1 = 0; static const int d2 = 1; }; class ChildClass: public BaseClass { char SomeChar = 'a'; long d22; void exFunc(int); //This method will be pulled up }; void ChildClass::exFunc(int) {};
class BaseClass { int d1 = 0; static const int d2 = 1; void exFunc(int); }; class ChildClass: public BaseClass { char SomeChar = 'a'; long d22; }; void BaseClass::exFunc(int) {};

Before

After

@interface SampleClass:NSObject /* This method will be pulled up */ - (int)IntSum:(int)num1 andNum2:(int)num2; @end @implementation SampleClass - (int)IntSum:(int)num1 andNum2:(int)num2 { int result = num1 = num2; return result; } @end
@interface BaseClass : NSObject - (int)IntSum:(int)num1 andNum2:(int)num2; @end @interface SampleClass : BaseClass @end @implementation BaseClass - (int)IntSum:(int)num1 andNum2:(int)num2 { int result = num1 = num2; return result; } @end

Before

After

class SuperClass: def super_method(self): pass class SubClassOne(SuperClass): # This function will be pulled up def my_method(self): pass
class SuperClass: def super_method(self): pass def my_method(self): pass class SubClassOne(SuperClass): pass

Pulling up the dependent members

Let's consider the following sample of C++ code:

class BaseClass { int a = 0; }; class ChildClass : BaseClass { int b1 = 12; int b2 = b1 * 4; };

As you can see, there is a dependency between variables bi and b2. If you apply Pull Members Up refactoring to class ChildClass and try to select b2 to be moved, and leave b1 in the class, CLion highlights the problem member in Pull Members Up dialog, as following:

Pull members up error 1

Trying to proceed with extract, you will get the following warning message:

Pull members up error 2

Choose Continue to ignore the problem and proceed with refactoring, or Cancel to return back and resolve it. Also you can observe the conflict in Find Tool Window.

Push members down

  1. In the editor, open the class whose members you need to push down.

  2. From the main or context menu, choose Refactor | Push Members Down. Push Members Down dialog displays the list of members to be pushed down.

  3. In the Members to be pushed down area, select the members you want to move. Note that the member at caret is already selected.

    If pushing a member might cause problems, you will be notified with red highlighting. It means that, if the situation is unattended, an error will emerge after refactoring. CLion prompts you with a Problems Detected dialog, where you can opt to ignore or fix the problem.

  4. Preview and apply changes.

Examples

Before

After

class BaseClass { int d1 = 0; static const int d2 = 1; void exFunc(int); //This method will be pushed down }; class ChildClass: public BaseClass { char SomeChar = 'a'; long d22; }; void BaseClass::exFunc(int) {};
class BaseClass { int d1 = 0; }; class ChildClass: public BaseClass { char SomeChar = 'a'; long d22; static const int d2 = 1; void exFunc(int); }; void ChildClass::exFunc(int) {};

Before

After

@interface Superclass : NSObject { int v; int w; //This variable will be pushed down } - (void)initV; @end @implementation Superclass - (void)initV { v = 20; } @end @interface Subclass : Superclass
@interface Superclass : NSObject { int v; } - (void)initV; @end @implementation Superclass - (void)initV { v = 20; } @end @interface Subclass : Superclass { int w; }

Before

After

class SuperClass: # This function will be pushed down def super_method(self): pass class SubClassOne(SuperClass): def my_method(self): pass class SubClassTwo(SuperClass): def my_method(self): pass
class SuperClass: pass class SubClassOne(SuperClass): def my_method(self): pass def super_method(self): pass class SubClassTwo(SuperClass): def my_method(self): pass def super_method(self): pass

Pushing down the dependant members

Let's consider the following sample of C++ code:

class SuperClass { int b1 = 12; int b2 = b1 * 4; }; class ChildClass : SuperClass { int b3 = 11; };

As you can see, variable b2 here depends on b1. If you apply Push Members Down refactoring to the above class and try to select b1 to be moved, and leave b2 in the base class class, CLion highlights the problem member in Pull Members Down dialog, as following:

Push members down error 2

Trying to proceed with extract, you will get the following warning message:

Push members down error 3

Choose Continue to ignore the problem and proceed with refactoring, or Cancel to return back and resolve it. Also you can observe the conflict in Find Tool Window.

Last modified: 25 July 2022