C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

friends and class templates

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
TheFerryman
Guest





PostPosted: Thu Mar 04, 2004 3:55 pm    Post subject: friends and class templates Reply with quote



The easiest way to describe this is with some code. In the following,
I would like the Thingalator::Func method to be able to access the
private member variable Thing::m_TList. Is it possible to make
Thingalator a friend of Thing? If so how?

Many thanks

//---------------------------------------------------------------------

#include <list>

template <class T>
class Thing
{
public:

typedef std::list<T> TList;

private:

TList m_TList;

public:

Thing(){}

};

template <class thing_type>
class Thingalator
{
thing_type m_Thing;

public:

void Func()
{
thing_type::TList local = m_Thing.m_TList;
}
};

int main()
{
Thingalator<Thing t;

t.Func();

return 0;
}

//---------------------------------------------------------------------
Back to top
Leor Zolman
Guest





PostPosted: Thu Mar 04, 2004 4:09 pm    Post subject: Re: friends and class templates Reply with quote



On Thu, 04 Mar 2004 15:55:47 +0000, TheFerryman <ferry (AT) onthenet (DOT) com> wrote:

Quote:
The easiest way to describe this is with some code. In the following,
I would like the Thingalator::Func method to be able to access the
private member variable Thing::m_TList. Is it possible to make
Thingalator a friend of Thing? If so how?

Many thanks

//---------------------------------------------------------------------

#include <list

Put this here:

template
(inform the compiler there's going to be a class template Thingalator)

Quote:

template <class T
class Thing
{

and now make that class a friend:

friend class Thingalator;


Quote:
public:

typedef std::list<T> TList;


[snip]

-leor

Leor Zolman
BD Software
[email]leor (AT) bdsoft (DOT) com[/email]
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html

Back to top
Leor Zolman
Guest





PostPosted: Thu Mar 04, 2004 4:22 pm    Post subject: Re: friends and class templates Reply with quote



On Thu, 04 Mar 2004 16:09:38 GMT, Leor Zolman <leor (AT) bdsoft (DOT) com> wrote:

Quote:
template <class T
class Thing
{

and now make that class a friend:

friend class Thingalator;


Sorry, that may have been more restrictive than you needed. This works more
generally:

friend class Thingalator;

-leor


Leor Zolman
BD Software
[email]leor (AT) bdsoft (DOT) com[/email]
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html

Back to top
TheFerryman
Guest





PostPosted: Thu Mar 04, 2004 4:32 pm    Post subject: Re: friends and class templates Reply with quote

On Thu, 04 Mar 2004 16:22:04 GMT, Leor Zolman <leor (AT) bdsoft (DOT) com> wrote:

Quote:
On Thu, 04 Mar 2004 16:09:38 GMT, Leor Zolman <leor (AT) bdsoft (DOT) com> wrote:

template <class T
class Thing
{

and now make that class a friend:

friend class Thingalator;

Thanks leor. This works.

Quote:


Sorry, that may have been more restrictive than you needed. This works more
generally:

friend class Thingalator;

But this more general case doesn't. I'm using MSVC6. Do you think
that's the reason?

I'd prefer to use this form because my Thingalator class may use any
class derived from a Thing.




Back to top
Leor Zolman
Guest





PostPosted: Thu Mar 04, 2004 5:08 pm    Post subject: Re: friends and class templates Reply with quote

On Thu, 04 Mar 2004 16:32:39 +0000, TheFerryman <ferry (AT) onthenet (DOT) com> wrote:

Quote:
On Thu, 04 Mar 2004 16:22:04 GMT, Leor Zolman <leor (AT) bdsoft (DOT) com> wrote:

On Thu, 04 Mar 2004 16:09:38 GMT, Leor Zolman <leor (AT) bdsoft (DOT) com> wrote:

template <class T
class Thing
{

and now make that class a friend:

friend class Thingalator;

Thanks leor. This works.



Sorry, that may have been more restrictive than you needed. This works more
generally:

friend class Thingalator;

But this more general case doesn't. I'm using MSVC6. Do you think
that's the reason?

Yes and no. I'm not sure that last way I wrote it is actually even legal;
it just happened to compile OK with VC7.1 in its default mode. Rewriting
it like this:

template<typename> friend class Thingalator;

works with VC71, Comeau and CodeWarrior, so I suspect that's more
standard-compliant. But that doesn't help you much in the case of pre-v7.1
MSVC...I can only think of ugly hack solutions that would generalize this
for VC6 (ones you'd probably resort to yourself if you had to), so I won't
post them. Perhaps someone else knows of an elegant way to deal with this
under VC6. Or perhaps you can use this as a justification to upgrade to 7.1
(a good idea if you're planning to do Stupid Template Tricks of any
significant variety...)
-leor


Quote:

I'd prefer to use this form because my Thingalator class may use any
class derived from a Thing.



Leor Zolman
BD Software
[email]leor (AT) bdsoft (DOT) com[/email]
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.