C++:如何在构造函数中创建接口数组
C++: How to create array of interfaces inside constructor
Arduino 项目。使用 C++ 11 编译器。我创建了一个 "interface" class 和几个实现 classes。我想实现策略模式。在我的策略管理器 class 中,我想创建一个固定长度的数组并在构造函数中对其进行初始化。
Java 我正在尝试做的事情的例子(在任何现代语言中都应该是小菜一碟,对吧 Stroustrup?)
public interface IState {
public void handle();
}
public class StateImpl implements IState {
@Override
public void handle(){
//Do something
}
}
public class StrategyManager {
private IState[] states;
public StrategyManager(){
states = new IState[3];
state[0] = new StateImpl();
state[1] = new StateImpl();
...
}
}
我在 C++ 中的第一次尝试:
IState.h:
class IState {
public:
virtual ~IState() {}
virtual void handle() = 0;
};
StateImpl.h:
#ifndef StateImpl_h
#define StateImpl_h
#include "IState.h"
class StateImpl : public IState {
public:
StateImpl();
virtual void handle() override;
};
#endif
StateImpl.cpp:
#include "StateImpl.h"
StateImpl::StateImpl(){}
void StateImpl::handle(){
//Do something
}
到目前为止一切正常。为了简洁起见,我简化了我的 classes,因此代码可能无法编译,但我的可以,现在问题来了:
StrategyManager.h:
#ifndef StrategyManager_h
#define StrategyManager_h
#include "IState.h"
class StrategyManager {
private:
extern const IState _states[3];
public:
StrategyManager();
};
#endif
StrategyManager.cpp:
#include "StrategyManager.h"
StrategyManager::StrategyManager(){
IState _states[3] = {
new StateImpl(),
new StateImpl(),
new StateImpl()
};
}
这给了我各种各样的错误:
error: storage class specified for '_states'
error: invalid abstract type 'IState' for '_states' because the following virtual functions are pure within 'IState':
virtual void IState::handle()
error: cannot declare field 'StrategyManager::_states' to be of abstract type 'IState'
since type 'IState' has pure virtual functions
... etc
所以我改变了数组来保存指针。在 StrategyManager.h:
extern const IState* _states[3];
现在在 StrategyManager.cpp 构造函数中:
StrategyManager::StrategyManager(){
IState impl = new StateImpl(); //I hope this will be stored in the heap.
IState* _states[3] = {
&impl,
&impl,
&impl
};
}
但仍然出现错误:
error: storage class specified for '_states'
error: cannot declare variable 'impl' to be of abstract type 'IState'
since type 'IState' has pure virtual functions
不断...
如何在不使用向量或增强或任何其他花哨的东西的情况下以简单的方式做到这一点? (记住这是 Arduino)
确实比那简单多了,而且更接近你的java代码(只显示相关部分):
class StrategyManager {
private:
IState *_states[3];
public:
StrategyManager();
};
StrategyManager::StrategyManager(){
_states[0] = new StateImpl();
_states[1] = new StateImpl();
_states[2] = new StateImpl();
};
}
记住,C/C++ 不是 java,没有 GC,所以清理你的对象
Arduino 项目。使用 C++ 11 编译器。我创建了一个 "interface" class 和几个实现 classes。我想实现策略模式。在我的策略管理器 class 中,我想创建一个固定长度的数组并在构造函数中对其进行初始化。
Java 我正在尝试做的事情的例子(在任何现代语言中都应该是小菜一碟,对吧 Stroustrup?)
public interface IState {
public void handle();
}
public class StateImpl implements IState {
@Override
public void handle(){
//Do something
}
}
public class StrategyManager {
private IState[] states;
public StrategyManager(){
states = new IState[3];
state[0] = new StateImpl();
state[1] = new StateImpl();
...
}
}
我在 C++ 中的第一次尝试:
IState.h:
class IState {
public:
virtual ~IState() {}
virtual void handle() = 0;
};
StateImpl.h:
#ifndef StateImpl_h
#define StateImpl_h
#include "IState.h"
class StateImpl : public IState {
public:
StateImpl();
virtual void handle() override;
};
#endif
StateImpl.cpp:
#include "StateImpl.h"
StateImpl::StateImpl(){}
void StateImpl::handle(){
//Do something
}
到目前为止一切正常。为了简洁起见,我简化了我的 classes,因此代码可能无法编译,但我的可以,现在问题来了:
StrategyManager.h:
#ifndef StrategyManager_h
#define StrategyManager_h
#include "IState.h"
class StrategyManager {
private:
extern const IState _states[3];
public:
StrategyManager();
};
#endif
StrategyManager.cpp:
#include "StrategyManager.h"
StrategyManager::StrategyManager(){
IState _states[3] = {
new StateImpl(),
new StateImpl(),
new StateImpl()
};
}
这给了我各种各样的错误:
error: storage class specified for '_states'
error: invalid abstract type 'IState' for '_states' because the following virtual functions are pure within 'IState':
virtual void IState::handle()
error: cannot declare field 'StrategyManager::_states' to be of abstract type 'IState'
since type 'IState' has pure virtual functions
... etc
所以我改变了数组来保存指针。在 StrategyManager.h:
extern const IState* _states[3];
现在在 StrategyManager.cpp 构造函数中:
StrategyManager::StrategyManager(){
IState impl = new StateImpl(); //I hope this will be stored in the heap.
IState* _states[3] = {
&impl,
&impl,
&impl
};
}
但仍然出现错误:
error: storage class specified for '_states'
error: cannot declare variable 'impl' to be of abstract type 'IState'
since type 'IState' has pure virtual functions
不断...
如何在不使用向量或增强或任何其他花哨的东西的情况下以简单的方式做到这一点? (记住这是 Arduino)
确实比那简单多了,而且更接近你的java代码(只显示相关部分):
class StrategyManager {
private:
IState *_states[3];
public:
StrategyManager();
};
StrategyManager::StrategyManager(){
_states[0] = new StateImpl();
_states[1] = new StateImpl();
_states[2] = new StateImpl();
};
}
记住,C/C++ 不是 java,没有 GC,所以清理你的对象