admin管理员组文章数量:1026989
I have two classes: Motor_Distance
and Motor_Displacement
that both contain identical code, however the type of the member variable count
and the return type of the accessor convToCount
is the only difference.
In Motor_Displacement
, count
is a long
since it can be both negative and positive.
In Motor_Distance
, count
is a unsigned long
since it cannot be negative. Thus it is unsigned so i can store higher positive values of count
Here is the code:
constexpr double pi = 3.14159;
class Motor_Distance{
private:
unsigned long count;
float wheel_diameter;
unsigned int pulses_per_revolution;
public:
unsigned long convToCount(){ return count; }
float convToMeters(){
return convToCount() * pi * wheel_diameter / pulses_per_revolution;
}
};
class Motor_Displacement{
long count;
float wheel_diameter;
unsigned int pulses_per_revolution;
public:
long convToCount(){ return count; }
float convToMeters(){
return convToCount() * pi * wheel_diameter / pulses_per_revolution;
}
};
int main(){
}
Since both classes contain repeated code, and I want to remove this repeated code. I could achieve this using a template base class as follows:
constexpr double pi = 3.14159;
template<typename Travel_Type>
class Motor_Travel{
Travel_Type count;
float wheel_diameter;
unsigned int pulses_per_revolution;
public:
Travel_Type convToCount(){ return count; }
float convToMeters(){
return convToCount() * pi * wheel_diameter / pulses_per_revolution;
}
};
class Motor_Distance : public Motor_Travel<unsigned long>{};
class Motor_Displacement : public Motor_Travel<long>{};
int main(){
}
I there a way I can do a similar thing (group/remove repeated code) without using templates?
I have two classes: Motor_Distance
and Motor_Displacement
that both contain identical code, however the type of the member variable count
and the return type of the accessor convToCount
is the only difference.
In Motor_Displacement
, count
is a long
since it can be both negative and positive.
In Motor_Distance
, count
is a unsigned long
since it cannot be negative. Thus it is unsigned so i can store higher positive values of count
Here is the code:
constexpr double pi = 3.14159;
class Motor_Distance{
private:
unsigned long count;
float wheel_diameter;
unsigned int pulses_per_revolution;
public:
unsigned long convToCount(){ return count; }
float convToMeters(){
return convToCount() * pi * wheel_diameter / pulses_per_revolution;
}
};
class Motor_Displacement{
long count;
float wheel_diameter;
unsigned int pulses_per_revolution;
public:
long convToCount(){ return count; }
float convToMeters(){
return convToCount() * pi * wheel_diameter / pulses_per_revolution;
}
};
int main(){
}
Since both classes contain repeated code, and I want to remove this repeated code. I could achieve this using a template base class as follows:
constexpr double pi = 3.14159;
template<typename Travel_Type>
class Motor_Travel{
Travel_Type count;
float wheel_diameter;
unsigned int pulses_per_revolution;
public:
Travel_Type convToCount(){ return count; }
float convToMeters(){
return convToCount() * pi * wheel_diameter / pulses_per_revolution;
}
};
class Motor_Distance : public Motor_Travel<unsigned long>{};
class Motor_Displacement : public Motor_Travel<long>{};
int main(){
}
I there a way I can do a similar thing (group/remove repeated code) without using templates?
本文标签:
版权声明:本文标题:c++ - How can I group the common code within two classes that have the exact same definitions but the type of one member variabl 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745658863a2161766.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论