nly*_*nly 1 c++ interface-design constructor abc
人们在这里使用C++抽象基类构造函数?我说的是没有数据成员且没有非纯虚拟成员的纯接口类.
任何人都可以用有用的方式展示任何使用ABC构造函数的习语吗?或者它只是固有的使用ABCs实现接口,它们保持空,内联和保护?
任何人都可以用有用的方式展示任何使用ABC构造函数的习语吗?
这是一个例子,虽然它是一个人为的,不常见的例子.
您可以使用它来保留所有实例的列表:
class IFoo
{
private:
//static members to keep a list of all constructed instances
typedef std::set<IFoo*> Set;
static Set s_set;
protected:
//new instance being created
IFoo()
{
s_set.insert(this);
}
public:
//instance being destroyed
virtual ~IFoo()
{
s_set.remove(this);
}
... plus some other static method and/or property
which accesses the set of all instances ...
};
Run Code Online (Sandbox Code Playgroud)
或者它只是固有的使用ABCs实现接口,它们保持空,内联和保护?
更常见的是,他们根本就没有宣布!没有理由声明它们: