Archived

This topic is now archived and is closed to further replies.

quasar3d

templated friend

Recommended Posts

quasar3d    814
When I have a templated class, I can't access the privat members of another class, that's an instasnce of the same template class, but instanciated with another type. Now I want to access the private values of this other classes, so I want to make all instances of the template friends of each other. I tried to put this in my class: template < class FRIENDTYPE > friend class POINTER< FRIENDTYPE >; but this doesn't work. How can I do this? My Site [edited by - Quasar3D on October 13, 2003 8:18:44 AM]

Share this post


Link to post
Share on other sites
Lord Bart    226
Hello quasar3d,

You can't do it.
Reason is, at time of template class template instanation the other types of templated classes are not known, how can they be?
You would have to pass in these other classes. and if you have a lot it would get very messy.

Plus you must also remember that not all methods are created ethier at instanation.
Only the ones that are call directly or indirectly(called by member methods of the template class of a directly called method). Although there might be a compiler option the create all methods.

Now the only way you could create this freind thing is by manual declearing a freind template passing in for each type the classes.
Thats the only way I can see it happen.

Lord Bart


[edited by - lord bart on October 13, 2003 9:07:27 AM]

Share this post


Link to post
Share on other sites
Matsen    132
I'm not sure I understand how your classes relate, but I think this will solve your problem.

template <typename T>
class A
{
int v_;
template <typename> friend class A; // A, with any type is a friend

public:

template <typename U>
void f(A<U>& a)
{
a.v_ = 123; // ok

}
};

Or, if the friend is another class (basically the same thing)

template <typename T>
class A
{
template <typename> friend class B;
int v_;
};

template <typename T>
class B
{
public:

template <typename U>
void f(A<U>& a)
{
a.v_ = 123;
}
};

I'm not sure, but I think VC++ 6.0 has some problems with this. Hope that helped.

Edit: spelling

[edited by - Matsen on October 13, 2003 4:00:55 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by quasar3d
but then I fuck op oop, and I love that.
That''s not true.. Neither Python nor CLOS have access restrictions, and they both support OO better than C++. These languages rely on the fact that other developers have some common sense. But sure, "#define private public" is something you shouldn''t do in C++ regardless of other languages; That''s just not the norm in C++.. So wait for other answers, maybe they''re more satisfactory.

Share this post


Link to post
Share on other sites
quasar3d    814
quote:
Original post by Matsen
I''m not sure I understand how your classes relate, but I think this will solve our problem.

template <typename T>
class A
{
int v_;
template <typename> friend class A; // A, with any type is a friend

public:

template <typename U>
void f(A<U>& a)
{
a.v_ = 123; // ok

}
};

Or, if the friend is another class (basically the same thing)

template <typename T>
class A
{
template <typename> friend class B;
int v_;
};

template <typename T>
class B
{
public:

template <typename U>
void f(A<U>& a)
{
a.v_ = 123;
}
};

I''m not sure, but I think VC++ 6.0 has some problems with this. Hope that helped.


Yes, this is what I want. Thank you. Too bad it indeed doesn''t work in vc6, but it does work in mingw, so I can at least blame vc6.

Does anyone know of an easy way to do this in vc6, or do you think I best just make everything public. I don''t like doing that, but if there''s no other way...


My Site

Share this post


Link to post
Share on other sites
the Speed Bump    240
You could make a base class from which all template instances derive, and give that some protected methods. Then make the superclass friends with whatever.

class AbstractThing
{
protected:
// wrapper methods here

}

template <class T>
class ConcreteThing : public AbstractThing
{
// do whatever, using AbstractThing methods when you need to take advantage of friendship

}

class IHaveLotsOfFriends
{
friend class AbstractThing;
}


Ugly, but it''ll do the job.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The obvious solution is to not make friend classes at all.

Write the appropriate accessors and mutators. Especially if you plan to use this code in any commercial software…

If these templates are simple classes that lots of other classes use... make them structs.

Data protection is the key to solid OO. Friend classes are a hack that was put into c++ before OO methodologies stabilized.


Practical reason why not to have friend classes:

You have a simple class... so you decide to make a few classes that acces this simple class a friend. All the friends access the private data members directly... Then OOPS! the specifications for your program changes. The simple class isn''t so simple any more. So now you have to re-write the simple class... then in turn NOW you have to re-write the all the friend classes that access the internal data. If you had accessors and mutatiors you could have gotten away with just re-writing the simple class...

Share this post


Link to post
Share on other sites