a friend to instanciate a base class with private ctor

Discussion in 'C++' started by nguillot, May 21, 2011.

  1. nguillot

    nguillot Guest

    Hello.

    I'm trying to forbid the instantiation of classes, except through a
    function whose role it is.
    I'm trying to do that by making the involved classes inherit from a
    class whose ctor is private.

    Something like that:

    template <typename T>
    T* create() { return new T(); }

    class noninstanciable : protected boost::noncopyable
    {
    template <typename T>friend T* create();
    // private ctor
    noninstanciable() {}
    };

    class MyClass : noninstanciable
    {
    public:
    MyClass() {}
    }

    int main()
    {
    MyClass * c = make<MyClass>();
    }

    But it doesn't work because of the private noninstanciable ctor.

    So I tried to make make<MyClass> as friend of noninstanciable like
    that:

    template <typename T>
    class noninstanciable : protected boost::noncopyable
    {
    friend T* create<T>();
    // private ctor
    noninstanciable() {}
    };

    class MyClass : noninstanciable<MyClass> { /* ... */ };

    But same issue.

    Finally I tried to make MyClass as a friend of noninstanciable (but if
    I succeeded MyClass would be directly instanciable...) like that:

    template <typename T>
    class noninstanciable : protected boost::noncopyable
    {
    friend typename T; // or also friend T or also friend class T
    // private ctor
    noninstanciable() {}
    };

    but it doesn't compile.

    So 2 questions:

    1) do you see a way to achieve what I try?
    2) how to make a template argument type of a class a friend of this
    class, ie:
    template <class TtoBeFriend>
    class X
    {
    friend X; // or whatelse?
    }

    Thanks for reply.
    nguillot, May 21, 2011
    #1
    1. Advertising

  2. nguillot

    nguillot Guest

    the last code snippet is:
    template <class TtoBeFriend>
    class X
    {
    friend TtoBeFriend; // or whatelse?
    -----------
    };

    and not "friend X" as in my original post...
    nguillot, May 21, 2011
    #2
    1. Advertising

  3. nguillot

    nguillot Guest

    I answer my last question.

    We can define a template arg as a class friend of antoher class with
    this "indirection":

    #include "loki/TypeManip.h"

    template <typename T>
    class X
    {
    friend class Loki::Type2Type<T>::OriginalType;
    };

    where Loki::Type2Type is:

    template <typename T>
    struct Type2Type
    {
    typedef T OriginalType;
    };
    nguillot, May 26, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cyril
    Replies:
    2
    Views:
    3,832
    cyril
    Aug 25, 2004
  2. Apricot
    Replies:
    4
    Views:
    511
    velthuijsen
    Apr 16, 2004
  3. Alf P. Steinbach
    Replies:
    6
    Views:
    531
    John Carson
    Sep 3, 2005
  4. NVH
    Replies:
    8
    Views:
    477
    mlimber
    Jul 6, 2006
  5. Grizlyk
    Replies:
    8
    Views:
    463
    Grizlyk
    Nov 29, 2006
Loading...

Share This Page