class Allocator {
public:
virtual char *allocate(unsigned int size)=0;
// EFF: allocates a character buffer of size characters
virtual void dispose(char *buf)=0;
// REQ: buf was allocated by this allocator
// EFF: release memory previously allocated.
};
class Translator {
public:
virtual char *operator()(const char *s, Allocator &a) = 0;
// EFF: returns a translation of the C-string s as
// another C-string, where the translation
// C-string is allocated by a.
};
假設你想實現以下:
void printTranslatedArgs(int argc, char *argv[],
Translator &t, Allocator &a);
// REQ: argc/argv are in the form of program arguments
// EFF: prints the translated command line.
我無法理解這是如何工作,因爲分配,部署,和運營商都是純虛擬的,所以他們是各自的類實際上並沒有定義這些功能。
實際上,我怎麼叫他們在這裏有關係嗎? – user1903336
因爲如果他們有函數定義你會使用它。 'char * p = a.allocate(5)'等 –