Declaring a template class with two template params a friend in anon-template class

Discussion in 'C++' started by A L, Aug 25, 2010.

  1. A L

    A L Guest

    I have an abstract class:

    class Attribute { /* ...*/ };

    Now I have another class:

    template <typename T, typename U>
    class Mgr
    {
    static U *create(const std::string k, std::string v)
    {
    static T *p = 0;
    if (!p)
    {
    p = new T();
    }
    T &r = *p;
    U *a = const_cast<U*>(r.get(k));
    return a;
    }
    };

    Both of these classes are declared/defined in different header files.

    The problem is that the constructors of Attribute class are private so
    I have to declare the Mgr class a friend of Attribute. That's what I
    have a problem doing - I am having a difficult time with the template
    syntax when it comes to declaring a template class (with two template
    parameters) a friend in a non-template class - I am getting lots of
    compilation errors on the friend template declaration. Can any kind
    sole tell me how I am supposed to do it? Declare a template class with
    two template parameters a friend in a non-template class???

    -Thanks/AL
    A L, Aug 25, 2010
    #1
    1. Advertising

  2. Re: Declaring a template class with two template params a friendin a non-template class

    * A L, on 25.08.2010 08:17:
    > I have an abstract class:
    >
    > class Attribute { /* ...*/ };
    >
    > Now I have another class:
    >
    > template<typename T, typename U>
    > class Mgr
    > {
    > static U *create(const std::string k, std::string v)
    > {
    > static T *p = 0;
    > if (!p)
    > {
    > p = new T();
    > }


    Above is unnecessary because C++ does it for you:

    static T *p = new T();


    > T&r = *p;
    > U *a = const_cast<U*>(r.get(k));


    This const_cast seems pretty dangerous.


    > return a;
    > }
    > };
    >
    > Both of these classes are declared/defined in different header files.
    >
    > The problem is that the constructors of Attribute class are private so
    > I have to declare the Mgr class a friend of Attribute. That's what I
    > have a problem doing - I am having a difficult time with the template
    > syntax when it comes to declaring a template class (with two template
    > parameters) a friend in a non-template class - I am getting lots of
    > compilation errors on the friend template declaration. Can any kind
    > sole tell me how I am supposed to do it? Declare a template class with
    > two template parameters a friend in a non-template class???


    Design Attribute as a singleton.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Aug 25, 2010
    #2
    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. James Aguilar
    Replies:
    5
    Views:
    455
    Rob Williscroft
    Nov 29, 2004
  2. Hicham Mouline
    Replies:
    0
    Views:
    330
    Hicham Mouline
    Nov 11, 2008
  3. Hicham Mouline
    Replies:
    1
    Views:
    347
    Vladyslav Lazarenko
    Mar 27, 2009
  4. Cotangent Alpha
    Replies:
    0
    Views:
    422
    Cotangent Alpha
    Jun 27, 2010
  5. Barry
    Replies:
    9
    Views:
    429
    Ara.T.Howard
    Sep 15, 2005
Loading...

Share This Page