如何在一个类中注入依赖项,这个类依赖于一个类,而这个类依赖于C++;? 我试图在C++项目中实现依赖注入。然而,由于依赖项的结构,我得到了一个无法解决的分段错误

如何在一个类中注入依赖项,这个类依赖于一个类,而这个类依赖于C++;? 我试图在C++项目中实现依赖注入。然而,由于依赖项的结构,我得到了一个无法解决的分段错误,c++,dependency-injection,C++,Dependency Injection,作为示例,我构建了以下类和接口。我有一个名为MyClass的类,它依赖于依赖关系。依赖项依赖于其他依赖项。为了进行适当的测试,我从接口继承了依赖项,即IDependency和IOtherDependency。OtherDependency有一个函数some_function() 在main.cpp中,我创建了MyClass的一个实例,然后尝试调用一些函数()。不幸的是,这会产生分段错误: Segmentation fault (core dumped) MyClass.h: #ifndef M

作为示例,我构建了以下类和接口。我有一个名为MyClass的类,它依赖于依赖关系。依赖项依赖于其他依赖项。为了进行适当的测试,我从接口继承了依赖项,即IDependency和IOtherDependency。OtherDependency有一个函数some_function()

在main.cpp中,我创建了MyClass的一个实例,然后尝试调用一些函数()。不幸的是,这会产生分段错误:

Segmentation fault (core dumped)
MyClass.h:

#ifndef MYCLASS_H
#define MYCLASS_H

#include "IDependency.h"

class MyClass
{
public:
    MyClass(IDependency *dependency);
    ~MyClass();

    IDependency *_dependency = nullptr;
};

#endif
MyClass.cpp:

#include "MyClass.h"

#include <iostream>

MyClass::MyClass(IDependency *dependency) : _dependency(dependency) {}

MyClass::~MyClass() {}
Dependency.cpp:

#include "Dependency.h"

#include <iostream>

Dependency::Dependency(IOtherDependency *other_dependency) : _other_dependency(other_dependency) {}

Dependency::~Dependency() {}
#include "OtherDependency.h"

#include <iostream>

OtherDependency::OtherDependency() {}

OtherDependency::~OtherDependency() {}

void OtherDependency::some_function()
{
    std::cout << "I am OtherDependency." << std::endl;
}
OtherDependency.h:

#ifndef DEPENDENCY_H
#define DEPENDENCY_H

#include "IDependency.h"
#include "IOtherDependency.h"

class Dependency : public IDependency
{
public:
    Dependency(IOtherDependency *other_dependency);
    ~Dependency();

    IOtherDependency *_other_dependency = nullptr;
};

#endif
#ifndef IDEPENDENCY_H
#define IDEPENDENCY_H

#include "IOtherDependency.h"

class IDependency
{
public:
    IOtherDependency *_other_dependency;
};

#endif
#ifndef OTHERDEPENDENCY_H
#define OTHERDEPENDENCY_H

#include "IOtherDependency.h"

class OtherDependency : public IOtherDependency
{
public:
    OtherDependency();
    ~OtherDependency();

    void some_function();
};

#endif
#ifndef IOTHERDEPENDENCY_H
#define IOTHERDEPENDENCY_H

class IOtherDependency
{
public:
    virtual void some_function() = 0;
};

#endif
OtherDependency.cpp:

#include "Dependency.h"

#include <iostream>

Dependency::Dependency(IOtherDependency *other_dependency) : _other_dependency(other_dependency) {}

Dependency::~Dependency() {}
#include "OtherDependency.h"

#include <iostream>

OtherDependency::OtherDependency() {}

OtherDependency::~OtherDependency() {}

void OtherDependency::some_function()
{
    std::cout << "I am OtherDependency." << std::endl;
}
main.cpp:

int main()
{
    OtherDependency *other_dependency = new OtherDependency;
    Dependency *dependency = new Dependency(other_dependency);
    MyClass my_class(dependency);

    my_class._dependency->_other_dependency->some_function();
}

我做错了什么/我需要更改吗?

您有两个变量称为
\u other\u dependency
:一个在
IDependency
中,另一个在
dependency
中。
Dependency
构造函数初始化了后者,而
IDependency
类中的构造函数保留其默认的nullptr值

当您访问
my_class.\u dependency->\u other_dependency
时,
other_dependency
将是
IDependency
中的一个,因为
\u dependency
指向基类

解决此问题的一种方法是从
依赖项
中删除
其他依赖项
,并将值从
依赖项
构造函数传递到
IDependency
,以正确初始化其成员