我想构建一个type::base
具有一些通用功能和流畅接口的基础(抽象)类(我们称之为),我面临的问题是所有这些方法的返回类型
class base {
public:
base();
virtual ~base();
base& with_foo();
base& with_bar();
protected:
// whatever...
};
现在我可以制作子类型,例如:
class my_type : public base {
public:
myType();
// more methods...
};
使用这样的子类型时会出现问题:
my_type build_my_type()
{
return my_type().with_foo().with_bar();
}
这不会编译,因为我们返回的是 base 而不是 my_type。
我知道我可以:
my_type build_my_type()
{
my_type ret;
ret.with_foo().with_bar();
return ret;
}
但是我在想如何实现它,但我没有找到任何有效的想法,一些建议?