slugg
Diamond Member
Hello there. I'm trying to make two template classes, where one is the friend of the other, but they use the same class. For example:
I want SluggFactory to be able to access SluggObject's private members and functions. This is trivial without templates (literally exactly what I have already, except take out the template stuff). A nested private class wouldn't work for me because the user needs to be able to directly use SluggObject objects.
I've googled around and the best I can really find is friend template functions to template classes. I'm talking about making the entire template class a friend of the other template class using the same type.
How to do? Thanks in advance 🙂
Edit: I've been a victim of sloppy typing/typos. For anyone else in Google-land looking for a solution to this problem, here it is:
Code:
template <class T>
class SluggFactory {
[INDENT]public: //functions here[/INDENT]
}
template <class T>
class SluggObject {
[INDENT]//members and other private stuff here
public:
friend class SluggFactory<T>;[/INDENT]
}
I want SluggFactory to be able to access SluggObject's private members and functions. This is trivial without templates (literally exactly what I have already, except take out the template stuff). A nested private class wouldn't work for me because the user needs to be able to directly use SluggObject objects.
I've googled around and the best I can really find is friend template functions to template classes. I'm talking about making the entire template class a friend of the other template class using the same type.
How to do? Thanks in advance 🙂
Edit: I've been a victim of sloppy typing/typos. For anyone else in Google-land looking for a solution to this problem, here it is:
Code:
[B]//You'll need a forward declaration of the friend class here, like so:
template <class T> SluggFactory<T>;[/B]
[B]//Now define the base class first:[/B]
template <class T>
class SluggObject {
[INDENT]//members and other private stuff here
public:
[B]template <class T> friend class SluggFactory;[/B][/INDENT]
}
[B]//Then define the friend class:[/B]
template <class T>
class SluggFactory {
[INDENT]public: //functions here[/INDENT]
}
Last edited: