friends and class templates

T

TheFerryman

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<int> > t;

t.Func();

return 0;
}

//---------------------------------------------------------------------
 
L

Leor Zolman

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<typename> class Thingalator;

(inform the compiler there's going to be a class template Thingalator)
template <class T>
class Thing
{

and now make that class a friend:

public:

typedef std::list<T> TList;

[snip]

-leor

Leor Zolman
BD Software
(e-mail address removed)
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
 
L

Leor Zolman

and now make that class a friend:

friend class Thingalator<Thing<T> >;

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

friend class Thingalator;

-leor


Leor Zolman
BD Software
(e-mail address removed)
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
 
T

TheFerryman

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?

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

Leor Zolman

Thanks leor. This works.


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

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

Leor Zolman
BD Software
(e-mail address removed)
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

templates and friends 1
friends 5
templates?? 2
friends, templates and comeau, gcc 4
templates and friends 7
Templates and g++ 4
friends and templates 1
Recursive templates 0

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top