What is the form of Templated Friend Class Variable another Templated Class?

AIterator ->Template Class
ASet ->Template Class

in the ASet.h(public section)(And AIterator.h included in ASet.h)
friend class AIterator<T>;

1
2
3
4
5
template<class T>
AIterator<T>::AIterator(ASet& friendVar)
{
	otherPtr = friendVar.sp;//sp is from protected section in ASet Class
}


What is the implementation of template class variable assigning another template class variable(which is friend class)?

Last edited on
Assuming that AIterator is the iterator class for ASet, this is what I would recommend:
make the iterator of ASet a member class of ASet. Then no special friend relationship would be required.

Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template < typename T > struct ASet
{
    // ...


    struct AIterator // AIterator is a member of ASet<T>
    {
         AIterator( ASet<T>& set ) ;

         // ...

         T* other_ptr ;
    };

    AIterator begin() { return AIterator(*this) ; }

    // ...

    protected: T* sp = nullptr ;

};

template < typename T >
ASet<T>::AIterator::AIterator( ASet<T>& set ) : other_ptr( set.sp ) {}


If that is not the case, see: https://isocpp.org/wiki/faq/templates#template-friends

-1
down vote
In my case this solution works correctly:

template <typename T>
class DerivedClass1 : public BaseClass1 {
template<class T> friend class DerivedClass2;
private:
int a;
};

template <typename T>
class DerivedClass2 : public BaseClass1 {
void method() { this->i;}
};
I hope it will be helpful.
http://www.cetpainfotech.com/technology/c-language-training
Topic archived. No new replies allowed.