semi-private constructors and inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jan Althaus

    semi-private constructors and inheritance

    I'm trying to solve the following problem:
    How do I let the user register a class with another class, without him
    being able to instantiate it?

    In a more practical way:
    The user derives from a Module class, which is able to perform actions
    which must only be performed during a given scope. This scope is
    managed by a different class, let's call it Manager. Because the
    derived class is not known beforehand, the user must be able to
    register the new type, however he must not be able to instantiate it
    himself (as that would defeat the whole purpose of managing it's
    scope).

    If it weren't for the polymorphism, one way to do this would be to
    have a manager method like this:

    template <class Tvoid RegisterModule( )
    {
    m_modules.push_ back( new T() );
    }

    To limit access one could make Module's constructor private and
    befriend Manager.
    So the usage would look something like this:

    Manager::Regist erModule< MyModule >();

    Given that people can derive from Module however, this means that the
    derived classes would have to, themselves, implement similar
    restrictions and again, befriend the manager themselves etc.
    All of which would make deriving from Module rather error prone and
    tedious.
    Can anyone think of a cleaner way to implement a similar model?
    Ta!

    Jan
  • blargg

    #2
    Re: semi-private constructors and inheritance

    In article
    <57d13560-6253-4d0d-9e14-773494ac1a5f@59 g2000hsb.google groups.com>, Jan
    Althaus <herrwurst@gmai l.comwrote:
    I'm trying to solve the following problem:
    How do I let the user register a class with another class, without him
    being able to instantiate it?
    This allows Creator to create objects of types derived from Base, but not
    for the user to create them, even though he defines them. But I question
    whether protecting the user from himself is really that big of a deal.

    // library

    class Key {
    private:
    Key() { }
    friend class Creator;
    };

    class Creator {
    public:
    template<class T>
    void create()
    {
    new T( Key() );
    }
    };

    class Base {
    protected:
    Base( Key const& );
    };

    // user

    class User : public Base {
    public:
    User( Key const& k ) : Base( k ) { }
    };

    void user( Creator& c )
    {
    c.create<User>( ); // OK, creator can create object
    new User( Key() ); // error, user can't create object
    }

    Comment

    • Jan Althaus

      #3
      Re: semi-private constructors and inheritance

      On Sep 21, 4:47 pm, blargg....@gish puppy.com (blargg) wrote:
      In article
      <57d13560-6253-4d0d-9e14-773494ac1...@59 g2000hsb.google groups.com>, Jan
      >
      Althaus <herrwu...@gmai l.comwrote:
      I'm trying to solve the following problem:
      How do I let the user register a class with another class, without him
      being able to instantiate it?
      >
      This allows Creator to create objects of types derived from Base, but not
      for the user to create them, even though he defines them. But I question
      whether protecting the user from himself is really that big of a deal.
      Ah, clever! That actually seems both very straight forward to derive
      from and implement. Ta!

      Comment

      Working...