Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Declaring a template class with two template params a friend in anon-template class

Reply
Thread Tools

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

 
 
A L
Guest
Posts: n/a
 
      08-25-2010
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      08-25-2010
* 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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring member function of another class as friend before it isdefined Cotangent Alpha C++ 0 06-27-2010 02:04 PM
declaring a friend template function whose arguments are of type = private member type Hicham Mouline C++ 1 03-27-2009 03:58 PM
declaring class member functions as friend without full class definition Hicham Mouline C++ 0 11-11-2008 09:19 PM
params v.s. @params in rails? Barry Ruby 9 09-15-2005 03:12 AM
Problem with declaring a template friend. James Aguilar C++ 5 11-29-2004 12:47 PM



Advertisments