帮我看看,友元,模板组合使用的问题?(可能还有缺省模板参数问题)

wukexin wukexin 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <iostream>
using namespace std;
//
class Byte
{
public:
Byte(const unsigned char c):ch_(c) {};
protected:
friend std::ostream& operator<< (std::ostream& out,const Byte& b);
private:
unsigned char ch_;
};
//
std::ostream& operator<< (std::ostream& out,const Byte& b)
{
for (int i = 7; i >= 0; i--)
{
if ( b.ch_ & (1 << i) )
out << '1';
else
out << '0';
}
return out;
}
//
template <typename T>
class MemoryMap
{
public:
MemoryMap(const T& x):data_(x) {};
protected:
friend std::ostream& operator<< (std::ostream& out, MemoryMap<T>& x);
private:
T data_;
};
//
template<typename T>
std::ostream& operator<< (std::ostream& out,MemoryMap<T>& x)
{
MemoryMap<T>* ptr=&x.data_;
size_t length=sizeof(x.data_);
Byte* p=reinterpret_cast<Byte*>(ptr);
for (size_t i=0;i<length;i++)
{
out<<*p++<<' ';
if (i>4 && i%4=0 ) out<<'\n';
}
return out;
}
//

int main()
{std::string s="\00a";

cout << s<< endl;
MemoryMap<std::string> mm(s);
cout<< mm<<endl;
return 0;
}
...全文
评论 12
收藏 已收藏
分享
返回顶部
wukexin
C++ 语言 65,214 成员 250,514 内容