By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 817 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Friends of Templates

P: n/a
Hi,

Can someone please tell me the correct syntax (if its possible of course)
to specify an output stream operator for a templated class so that I dont
need to write the same function for all the derived classes from the
template. I have a sample of what it thought it might look like below.
==BEGIN CODE SNIPPET===

template<class T1, class T2>
class MyDerived : public MyClass {
friend ostream& operator<< (ostream& os, MyDerived<T1, T2> * sa);
....
};

template<class T1, class T2>
ostream& operator<< (ostream& os, MyDerived<T1, T2> * sa) {
}

===END CODE SNIPPED====

Thanks in advance for any suggestions.

Cheers

Tom

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Tom McCallum" <te********@hotmail.com> wrote in message
news:op**************@news.blueyonder.co.uk...
Hi,

Can someone please tell me the correct syntax (if its possible of course)
to specify an output stream operator for a templated class so that I dont
need to write the same function for all the derived classes from the
template. I have a sample of what it thought it might look like below.
==BEGIN CODE SNIPPET===

template<class T1, class T2>
class MyDerived : public MyClass {
friend ostream& operator<< (ostream& os, MyDerived<T1, T2> * sa);
...
};

template<class T1, class T2>
ostream& operator<< (ostream& os, MyDerived<T1, T2> * sa) {
}

===END CODE SNIPPED====

Thanks in advance for any suggestions.


Not sure why the above code takes a pointer instead of the usual const
reference.

This is covered in the FAQ (http://www.parashift.com/c++-faq-lite/). See
section 34 ("Container classes and templates"), question 15 ("Why do I get
linker errors when I use template friends?").

--
David Hilsee
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.