我如何通过公共接口(C++)连接两个类(不认识彼此的类别)

发布于 2025-01-18 01:34:43 字数 1132 浏览 3 评论 0原文

我目前正在从事一个项目,其中一切都与所有内容都充满了混合。每个文件都包括其他文件等。 我想将此意大利面代码的分开部分集中在一个库中,该库必须完全独立于该代码的其余部分。

当前的问题是我库的某些函数函数内部使用某些函数 function external 在其他地方声明,因此我的库包括项目中包含的其他一些文件要求“独立于代码的其余部分”。

不用说,我无法在库中移动功能外部

解决此问题的第一个想法是实现一个公共界面,例如描述的Bellow:

“在此处输入image

但我可以' t使它起作用。是我的全局模式是我可以实现它的方式,还是在可能的情况下(如果可能)将两个函数接口而不在另一个文件中插入一个函数,从而导致不必要的依赖关系。

我如何抽象我的外部阶级,以便我的库仍然独立于我的代码的其余部分?

编辑1:

外部

#include "lib/InterfaceInternal.h"

class External : public InterfaceInternal {
private:
    void ExternalFunction() {};
public:

    virtual void InterfaceInternal_foo() override {
        ExternalFunction();
    };
 
};

#pragma once
#include "InterfaceInternal.h"

class Internal {

    // how can i received there the InterfaceInternal_foo overrided in External.h ? 

};

#pragma once

class InterfaceInternal {
public:
    virtual void InterfaceInternal_foo() = 0;
};

I'm currently working on a project where everything is horribly mixed with everything. Every file include some others etc..
I want to focus a separating part of this spaghetti code into a library which has to be completely independent from the rest of the code.

The current problem is that some functions FunctionInternal of my library use some functions FunctionExternal declared somewhere else, hence my library is including some other files contained in the project, which is not conform with the requirement "independent from the rest of the code".

It goes without saying that I can't move FunctionExternal in my library.

My first idea to tackle this problem was to implement a public interface such as described bellow :

enter image description here

But I can't get it to work. Is my global pattern a way I could implement it or is there another way, if possible, to interface two functions without including one file in another causing an unwanted dependency.

How could I abstract my ExternalClass so my library would still be independent of the rest of my code ?

Edit 1:

External.h

#include "lib/InterfaceInternal.h"

class External : public InterfaceInternal {
private:
    void ExternalFunction() {};
public:

    virtual void InterfaceInternal_foo() override {
        ExternalFunction();
    };
 
};

Internal.h

#pragma once
#include "InterfaceInternal.h"

class Internal {

    // how can i received there the InterfaceInternal_foo overrided in External.h ? 

};

InterfaceInternal.h

#pragma once

class InterfaceInternal {
public:
    virtual void InterfaceInternal_foo() = 0;
};

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

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

发布评论

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

评论(1

薄凉少年不暖心 2025-01-25 01:34:43

您可以像建议的那样做,覆盖外部代码中的内部接口。然后

//我如何在那里收到interfaceInternal_foo在external.h?

中覆盖

只需将指针/引用传递给您的类外部,它扩展了类InterfaceInternal。当然,您的类内部需要具有接受InterfaceNternal*的方法。

或者,您可以将函数传递到内部界面作为参数。周围的东西:

class InterfaceInternal {
public:
    void InterfaceInternal_foo(std::function<void()> f);
};

或更多通用:

class InterfaceInternal {
public:
    template <typename F>  // + maybe some SFINAE magic, or C++20 concept to make sure it's actually callable
    void InterfaceInternal_foo(F f);
};

You can do like you suggested, override the internal interface in your external code. Then

// how can i received there the InterfaceInternal_foo overrided in External.h ?

just pass a pointer/reference to your class External that extends class InterfaceInternal. Of course your class Internal needs to have methods that accept InterfaceInternal*.

Or you can just pass the function to your internal interface as an argument. Something around:

class InterfaceInternal {
public:
    void InterfaceInternal_foo(std::function<void()> f);
};

or more generic:

class InterfaceInternal {
public:
    template <typename F>  // + maybe some SFINAE magic, or C++20 concept to make sure it's actually callable
    void InterfaceInternal_foo(F f);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文