尝试在 C++ 中很少设计模式
attempt to little design pattern in c++
我正在学习 C++。 我非常了解Java和Python,但是在我的C ++试验中使用一些花哨的设计模式让我很生气。
这是我的文件,我认为它们非常自动解释,如果有什么问题,欢迎评论!
我有一个具有行为的类引擎..我想将行为代入不同的特定行为..但是没有子类也很容易。
主要.cpp :
#include "Engine.h"
int main() {
Engine e;
e.work();
return 0;
};
发动机:
#ifndef ENGINE_H_
#define ENGINE_H_
#include <iostream>
#include "Behaviour.h"
class Engine {
public:
Engine() {
std::cout << "Engine constructor" << std::endl;
this->b = new Behaviour(this);
};
virtual ~Engine(){};
void work() {
std::cout << "Engine work" << std::endl;
};
int getFoo() { return 42; };
private:
Behaviour * b;
};
#endif /* ENGINE_H_ */
行为.h :
#ifndef BEHAVIOUR_H_
#define BEHAVIOUR_H_
#include <iostream>
#include "Engine.h"
class Behaviour {
public:
Behaviour(Engine* e) {
std::cout << "behaviour constructor, kind of abstract class" << std::endl;
this->e = e;
};
virtual ~Behaviour(){};
void work() {
std::cout << "Behaviour work" << this->e->getFoo() << std::endl;
};
protected:
Engine * e;
};
#endif /* BEHAVIOUR_H_ */
我的编译器错误:
$ rm *.gch; c++ *
In file included from Behaviour.h:5:
Engine.h:26: error: ISO C++ forbids declaration of ‘Behaviour’ with no type
Engine.h:26: error: expected ‘;’ before ‘*’ token
Engine.h: In constructor ‘Engine::Engine()’:
Engine.h:14: error: ‘class Engine’ has no member named ‘b’
Engine.h:14: error: expected type-specifier before ‘Behaviour’
Engine.h:14: error: expected ‘;’ before ‘Behaviour’
In file included from Engine.h:5:
Behaviour.h:11: error: expected ‘)’ before ‘*’ token
Behaviour.h:23: error: ISO C++ forbids declaration of ‘Engine’ with no type
Behaviour.h:23: error: expected ‘;’ before ‘*’ token
Behaviour.h: In member function ‘void Behaviour::work()’:
Behaviour.h:19: error: ‘class Behaviour’ has no member named ‘e’
Engine.h: In constructor ‘Engine::Engine()’:
Engine.h:14: error: no matching function for call to ‘Behaviour::Behaviour(Engine* const)’
Behaviour.h:7: note: candidates are: Behaviour::Behaviour()
Behaviour.h:7: note: Behaviour::Behaviour(const Behaviour&)
我想我需要一些前向声明,但我不确定,浏览教程并进行一些试验根本无法解决我的问题。
在Engine.h
中,将包含替换为前向声明:
#include "Behaviour.h"
跟
class Behaviour;
Behavior.h
内部的class Engine;
也一样。
由于具有循环依赖项,因此需要使用前向声明而不是包含。
您还需要在 cpp
文件中分离实现。仅靠转发声明不会让您致电this->b = new Behaviour(this);
.
另外,花一些时间重新考虑您的设计。这种依赖性通常是一种代码气味。
您遇到了循环依赖问题,通常,您必须在 CPP 文件中实现行为的方法,但这可能不是最佳实践。我认为引擎在您的应用程序中是一个非常重要的抽象概念,您可以使用纯虚拟类作为接口定义来描述它。
例如:
IEngine.h
class IEngine {
public:
void work() = 0;
};
IEngine* newEngine();
EngineImpl.h
class EngineImpl : public IEngine {
public:
void work();
};
发动机.cpp
void EngineImpl::work();
// do some works
}
IEngine* newEngine() {
return new EngineImpl();
}
现在,您可以通过包含 IEngine.h 在代码中的任何位置使用 IEngine 引擎,甚至可以通过定义以下内容来插入引擎单例:
IEngine.h:
IEngine* sharedEngine();
发动机.cpp
IEngine* sharedEngine() {
static IEngine *engineInst = 0;
if (!engineInst)
engineInst = new EngineImpl();
return engineInst;
}
相关文章:
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 资源管理设计模式
- 用于在回调中调用解析器的设计模式
- 设计帮助 - 为不同类型的消息处理通用接口的设计模式
- 在这种情况下我应该使用哪种设计模式
- C++中物体改变识别的设计模式?
- 确保所有构造函数调用相同的函数 c++ 设计模式
- 需要实例化不同类/对象并在启动时确定的硬件插槽的设计模式
- 设计模式,以避免不必要地添加抽象函数以适应新功能
- 工厂设计模式优化
- 使用C++模板的数据映射器设计模式
- 为什么以及如何使用原型设计模式
- 具有多个继承共享一个资源的对象 - 寻找良好的设计模式
- 在C++中创建观察器设计模式的好方法
- 现代C++在多大程度上消除了对设计模式的需求?
- 对于存储另一个类所需信息的类,例如其构造,是否有设计模式?
- 下面抽象工厂设计模式的实现是正确的吗
- sql记录集函数的状态设计模式
- 是否有可以处理方法调用依赖关系的设计模式?
- 使用 C++ 设计模式