【发布时间】:2011-06-05 19:49:54
【问题描述】:
我想知道如何在我的应用程序中记录一个接口IResource。由于我编写的是引擎而不是库,因此我认为文档应该提供有关如何编写接口实现的指南;可以吗?
另外,请您看看我的界面,告诉我cmets是否足够清晰?
/**
Interface that should be implemented by all resources. Implementing
this interface is necessary for compatibility with the ResourceManager
class template.
\note Documentation of this interface includes guidelines on how
implementations should be written.
\see ResourceManager
*/
class IResource
{
public:
/**
Loads resource data from a file. If data is already loaded,
the function should return immediately.
\throw std::exception Should throw on any failure to load the
resource. If the resource is already loaded, don't throw, just
return (as previously indicated).
\note Access to this function should also be provided directly
from a constructor. That constructor should catch any exceptions
and throw them further to its caller.
*/
virtual void loadFromFile(const std::string& file) = 0 ;
/**
All general guidelines from loadFromFile() also apply to this
function. Additionally, the resource should not take possession of
the buffer; the buffer should be safe to delete after loading.
*/
virtual void loadFromMemory(const char* buffer, std::size_t size) = 0;
/**
Frees the data currently held by the resource object. Should
return immeditelly if no data is loaded.
*/
virtual void free() = 0;
virtual bool isLoaded() const = 0;
};
编辑:开启了相关讨论。
主要遵循Johann Gerell's answer 的cmets 部分的对话,我在programmers.stackexchange 上打开了一个相当长的线程。你可以在这里查看:
> Single-responsibility and custom data types
【问题讨论】:
-
你知道不能在构造函数中调用虚方法吗?
-
显然你不会在构造函数中调用
IResource::loadFromFile()。例如,在其构造函数中,Font将调用它自己的该函数的实现,它编译得很好。 -
如果您从 Font 派生并编写新版本会怎样。那么 Font(s) 构造函数中的版本将不会调用正确的版本。阅读下面的@Johann Gerell,了解将
loading和being资源划分为不同的类。 -
@Martin 你的意思是这样吗? ideone.com/ni9u3 似乎工作正常。
-
@Martin:不能在基类构造函数中调用虚函数。从派生类构造函数中,它工作得很好。
标签: c++ interface documentation