468,780 Members | 2,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,780 developers. It's quick & easy.

How do I write this function only once using templates?

How do I use a template to write these friend functions once? The are nearly
identical. Also, why does wcout << L'A'; print 65 instead of a character?
Thanks,
Siegfried

struct DVDDeltaTime{
// constructors go here...
friend std::ostream & operator<<(std::ostream &os, const DVDDeltaTime&
dt ){
return os
<< ' ' << std::setw(2) << std::setfill('0') << dt.m_nTitles
<< ':' << std::setw(2) << std::setfill('0') << dt.m_nHours
<< ':' << std::setw(2) << std::setfill('0') << dt.m_nMinutes
<< ':' << std::setw(2) << std::setfill('0') << dt.m_nSeconds
<< ':' << std::setw(2) << std::setfill('0') << dt.m_nFrames;
}
friend std::wostream & operator<<(std::wostream &os, const DVDDeltaTime&
dt ){
return os
<< L" " << std::setw(2) << std::setfill(L'0') << dt.m_nTitles
<< L":" << std::setw(2) << std::setfill(L'0') << dt.m_nHours
<< L":" << std::setw(2) << std::setfill(L'0') << dt.m_nMinutes
<< L":" << std::setw(2) << std::setfill(L'0') << dt.m_nSeconds
<< L":" << std::setw(2) << std::setfill(L'0') << dt.m_nFrames ;
}
private:
unsigned m_nTitles:8, m_nHours:6, m_nMinutes:6, m_nSeconds:6,
m_nFrames:6;
}
Jul 22 '05 #1
2 1869

"Siegfried Heintze" <si*******@heintze.com> wrote in message
news:40********@news.seqnet.net...
How do I use a template to write these friend functions once? The are nearly identical. Also, why does wcout << L'A'; print 65 instead of a character? Thanks,
Siegfried


template<typename Ch, typename Tr>
friend std::basic_ostream<Ch, Tr>& operator<<
(std::basic_ostream<Ch, Tr>& out, DVDDeltaTime) { /* ... */ }

Be aware that not all compilers are happy with this.

Jonathan
Jul 22 '05 #2
"Siegfried Heintze" <si*******@heintze.com> wrote in
news:40********@news.seqnet.net:

(...)
Also, why does wcout << L'A'; print 65 instead of a
character?


It's actually off topic... but anyway...
This is a "MSVC++ Specific Behaviour(TM)" - they don't sport wchar_t as a
unique type, but rather a typedef from unsigned short. poof!

Cheers,
b
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by David Furey | last post: by
9 posts views Thread by Fred H | last post: by
7 posts views Thread by Hendrik Schober | last post: by
13 posts views Thread by Stumped and Confused | last post: by
18 posts views Thread by jacob navia | last post: by
10 posts views Thread by roberts.noah | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.