c++constructorabstract-base-classinterface-design

What do *you* use C++ ABC constructors for?


What do people here use C++ Abstract Base Class constructors for in the field? I am talking about pure interface classes having no data members and no non-pure virtual members.

Can anyone demonstrate any idioms which use ABC constructors in a useful way? Or is it just intrinsic to the nature of using ABCs to implement interfaces that they remain empty, inline and protected?


Solution

  • Can anyone demonstrate any idioms which use ABC constructors in a useful way?

    Here's an example, although it's a contrived, uncommon example.

    You might use it to keep a list of all instances:

    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 ...
    };
    

    Or is it just intrinsic to the nature of using ABCs to implement interfaces that they remain empty, inline and protected?

    More usually they're just not declared at all! There's no reason to declare them: