【发布时间】:2014-07-30 15:57:52
【问题描述】:
阅读Effective C++,第 31 条描述了两种方法,通过让客户端使用一个类来最小化重新编译时间,该类只为他们可以调用的函数提供接口。此类将这些函数调用引用到其他执行实际工作的类。
描述的第一个方法是 Handle 类,它使用 pImpl 习惯用法将函数调用转发到实现类,该类包含它正在执行的所有操作的实现。 Handle 类有一个指向这种类型的实例化对象的指针,它的函数调用该对象上的相应函数并返回它们的值。
我遇到的问题是第二个,它是使用具有所有纯虚函数的基类实现的。在这个方法中,基类定义了允许调用的函数,但是它们都被声明为纯虚函数,所以派生类才是真正实现它们的。客户端具有指向派生类对象的基类的指针或引用,并调用这些函数,这些函数映射到派生类的已实现函数。这本书描述了让基类有一个工厂函数,它返回一个指向派生类的指针。从书中的例子来看,如果 Person 是基类,RealPerson 是派生类,客户端应该能够像这样创建一个对象:
std::string name;
Date dateOfBirth;
Address address;
//create an object supporting the Person interface
std::tr1::shared_ptr<Person> pp(Person::create(name, dateOfBirth, address);
当我尝试实现类似的东西,围绕一个 Rational 类构建时,我遇到了问题,但是:
class Rational;
class RationalBase {
public:
virtual ~RationalBase ();
static RationalBase* create(int top, int bottom) {
RationalBase* ptr = new Rational(top, bottom);
return ptr;
}
virtual int getNumerator () const = 0;
virtual int getDenominator () const = 0;
virtual void setNumerator (int) = 0;
virtual void setDenominator (int) = 0;
};
class Rational : public RationalBase {
public:
Rational (int top, int bottom) : numerator(top), denominator(bottom) {}
virtual ~Rational () {}
virtual int getNumerator () const {
return numerator;
}
virtual int getDenominator () const {
return denominator;
}
virtual void setNumerator (int top) {
numerator = top;
}
virtual void setDenominator (int bottom) {
denominator = bottom;
}
private:
int numerator;
int denominator;
};
g++ 编译器在create 函数内的RationalBase* ptr = new Rational(top, bottom); 行上给了我错误invalid use of incomplete type ‘struct Rational’。
谷歌搜索这个错误,我发现this question 解释了这样做的原因是你只能对前向声明的类做很多事情,而创建它们的新实例不是其中之一。但是我不明白Interface类应该如何实现?
【问题讨论】: