我有一个抽象基类,它定义了数据接口的接口。数据汇的具体实现是通过工厂获取的。为了整理代码,我为factory方法创建了一个typedef,它从DataSink抽象基类中返回新的DataSink对象。使用typedef函数指针的不完整类型
#include <memory>
#include <string>
class DataSink
{
public:
DataSink() { }
virtual ~DataSink() { }
void Open(const std::string path)
{
InternalOpen(path);
}
bool IsOpen()
{
return InternalIsOpen();
}
void Write(const uint8_t* data, const size_t offset, const size_t size)
{
InternalWrite(data, offset, size);
}
void Close()
{
InternalClose();
}
protected:
virtual void InternalOpen(const std::string path) = 0;
virtual bool InternalIsOpen() = 0;
virtual void InternalWrite(const uint8_t* data, const size_t offset, const size_t size) = 0;
virtual void InternalClose() = 0;
};
typedef std::auto_ptr<DataSink>(*get_new_data_sink_function_type)(std::string);
如果我再尝试声明:某处
boost::function<get_new_data_sink_function_type> getNewDataSinkFunction_;
在路上,我得到:
error: field 'getNewDataSinkFunction_' has incomplete type
如果我不是声明:
boost::function<std::auto_ptr<DataSink>(std::string)> getNewDataSinkFunction_;
......一切都很好。
我意识到DataSink是一个不完整的类型,因为它是抽象的,但因为我使用引用语义由于std :: auto_ptr,应该是好的,对吗?无论如何,这并不能解释typedef为什么会失败,并且typedef的定义粘贴被剪切成功。这是一个具有boost :: function的怪癖吗?
编译器是gcc 4.3.3。任何见解非常感谢。
'size_t'参数上的'const'限定符确实不是必需的,删除它们会使代码更好地适合问题(没有水平滚动条)。 – 2011-06-08 05:42:52
你是对的,因为它们是通过价值传递的。我知道'const'的const限定参数已经变成了一个反射......有点过于热情了:) – 2011-06-08 22:41:22