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
Select the members to be moved to a superclass.
From the main or context menu, call
.In the Pull members up dialog, specify the following:
Select the superclass.
In the Member section, select the members to be moved.
CLion will highlight the dependent members:
Click Preview to check the changes before proceeding. CLion will notify you in case of problems:
Click Pull when ready.
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 |
Push members down
Select the members to be moved to a subclass.
From the main or context menu, call
.In the Push Members Down dialog, specify the following:
In the Inheritor section, select the subclass.
In the Member section, select the members to be moved.
Click Preview to check the changes before proceeding.
Click Push when ready.
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 |