(c++)两个具有一些共同功能的类。最清洁的代码方式

发布于 2025-02-10 21:35:32 字数 1830 浏览 0 评论 0原文

我有两个类,具有一些常见的功能和一些不同的功能。

假设

class Red{
    public:
        void funcA();
        void funcC();
}

class Blue{
    public:
        void funcB();
        void funcC();
}

注意我的实际代码包含更多功能(既可以使用通用函数和非命令),

并且我需要为

  1. 初始化上面两个类之一的接口上一个类。
  2. 包含以上类在上面的每个函数的函数,如果该类可用于该类,

则是示例

interface.cpp

Red *red_object = nullptr;
Blue *blue_object = nullptr;

void init(int mode){
    if (mode == 0) red_object = new Red();
    else blue_object = new Blue();
}

void run_func_a(){
    if (mode == 0) red_object->funcA();
}

void run_func_b(){
    if (mode == 1) blue_object->funcB();
}

void run_func_c(){
    if (mode == 0) {
        red_object->funcC();
    }
    else {
        blue_object->funcC();
    } 
}

问题是,我认为它非常笨拙(例如,run_func_c( ))当我必须为每个函数编写它时,我想以某种方式将其概括,例如使用 sasonitance 。但是,我无法使用它,因为两个类中都不存在一些功能。我可以填写一个空功能,而没有它的功能,但是从长远来看,它不是很好。

有没有更好的方法以更精确,更清洁的方式构造接口文件?

编辑:

如@adrianmole所述,我想澄清我想象的。

我将拥有一个基类颜色

class Colour{
    void funcC();
}

class Red: public Colour{
    void funcA();
}

但是,当我想在 interface.cpp 中编写功能时,

Colour colour_object = nullptr;
void init(int mode){
    if(mode ==0) colour_object = new Red();
    else(mode == 0) colour_object = new Blue();
}

void run_func_a(){
    colour_object->funcA(); // This will have error
}

void run_func_c(){
    colour_object->funcC(); // This is okay and looks clean.
}

colour_object-> funca()()将会引起错误,因为它在基类中不存在。

我可以在基类中添加funca(),但是想象一下,如果我有10个常见功能,那么RED和10个功能的10个功能和10个功能是blue。我认为这将在基类中发挥很多作用。 (尽管这是最好的方法,但我可能会介绍这种方法)

I have two classes that has some common functions and some different functions.

Let's say

class Red{
    public:
        void funcA();
        void funcC();
}

class Blue{
    public:
        void funcB();
        void funcC();
}

Note My actual code contains more functions (both common function and non-common one)

and I need to make a class for an interface that

  1. initialize one of the two classes above.
  2. contains the function to run each function in the class above if it is available for that class

Here is the example

interface.cpp

Red *red_object = nullptr;
Blue *blue_object = nullptr;

void init(int mode){
    if (mode == 0) red_object = new Red();
    else blue_object = new Blue();
}

void run_func_a(){
    if (mode == 0) red_object->funcA();
}

void run_func_b(){
    if (mode == 1) blue_object->funcB();
}

void run_func_c(){
    if (mode == 0) {
        red_object->funcC();
    }
    else {
        blue_object->funcC();
    } 
}

The problem is, I think it is very clunky (e.g., run_func_c()) when I have to write it for every function so I want to somehow generalize it, like using inheritance. However, I cannot use it since there is some function that does not exist in both classes. I could fill in an empty function to the one that does not have it but it is not good in the long term.

Is there a better way to construct the interface file in a more precise and cleaner way?

Edit:

I would like to clarify what I imagine in case of inheritance as @AdrianMole mentioned.

I will have a base class Colour.

class Colour{
    void funcC();
}

class Red: public Colour{
    void funcA();
}

But when I want to write the function in interface.cpp,

Colour colour_object = nullptr;
void init(int mode){
    if(mode ==0) colour_object = new Red();
    else(mode == 0) colour_object = new Blue();
}

void run_func_a(){
    colour_object->funcA(); // This will have error
}

void run_func_c(){
    colour_object->funcC(); // This is okay and looks clean.
}

colour_object->funcA() will raise an error since it doesn't exist on the base class.

I can just add funcA() in base class, but imagine if I have like 10 common functions, 10 functions unique to Red and 10 functions unique to Blue. I think that will be a lot of function in base class. (Although if it is the best approach, I might set on this approach)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始于初秋 2025-02-17 21:35:32

使用虚拟关键字来存档。

例子:

class Color
{
public:
        void funcColor()
        {
            cout<<"Color::funcColor()\n";
        }
  virtual void funcC()
        {
            cout<<"Color::funcC()\n";
        }
};

class Red : public Color{
    public:
        void funcA()
        {
            cout<<"Red::funcA()\n";
        }
         void funcC()
        {
            cout<<"Red::funcC()\n";
        }
};

class Blue : public Color{
    public:
        void funcB()
        {
             cout<<"Blue::funcB()\n";
        }
         void funcC()
        {
             cout<<"Blue::funcC()\n";
        }
};

int main()
{
    Color *color;
    Blue blue;
    Red red;

    color=&blue;
    color->funcC();
    color=&red;
    color->funcC();

    return 0;
}

Use virtual keyword to archive this.

Example:

class Color
{
public:
        void funcColor()
        {
            cout<<"Color::funcColor()\n";
        }
  virtual void funcC()
        {
            cout<<"Color::funcC()\n";
        }
};

class Red : public Color{
    public:
        void funcA()
        {
            cout<<"Red::funcA()\n";
        }
         void funcC()
        {
            cout<<"Red::funcC()\n";
        }
};

class Blue : public Color{
    public:
        void funcB()
        {
             cout<<"Blue::funcB()\n";
        }
         void funcC()
        {
             cout<<"Blue::funcC()\n";
        }
};

int main()
{
    Color *color;
    Blue blue;
    Red red;

    color=&blue;
    color->funcC();
    color=&red;
    color->funcC();

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文