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

wukexin 2009-12-14 07:14:57
#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;
}
...全文
110 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
baihacker 2009-12-17
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 wukexin 的回复:]
baihacker
不好意思,结贴时疏忽了。
[/Quote]
没关系.
wukexin 2009-12-17
  • 打赏
  • 举报
回复

baihacker
不好意思,结贴时疏忽了。
lovesi3344 2009-12-15
  • 打赏
  • 举报
回复
关注
macrojj 2009-12-15
  • 打赏
  • 举报
回复
注意赋值左右的类型
hoomien 2009-12-15
  • 打赏
  • 举报
回复
学习
wukexin 2009-12-15
  • 打赏
  • 举报
回复
为什么得到的结果,那么奇怪!(只有int类型结果可以理解)

int main()
{
char* s="0a";
WKX::MemoryMap<unsigned char> c1='0';
WKX::MemoryMap<unsigned char> c2='a';
WKX::MemoryMap<char*> mm(s);
cout<<
c1<<'\n'<<
c2<<'\n'<<
s<<'\t'<<
sizeof(char)<<'\t'<<
sizeof(s)<<'\t'<<
mm<<endl;
int x=5;
WKX::MemoryMap<int> i(x);
cout<<"sizeof(WKX::MemoryMap<int>)="<<sizeof(i)<<"\n"
<<x<<"=\n"<<i<<"\n";
WKX::MemoryMap<int> si(-x);
cout<<-x<<"=\n"<<si<<"\n";
float y=0.1;
WKX::MemoryMap<float> f(y);
cout<<"sizeof(WKX::MemoryMap<float>)="<<sizeof(f)<<"\n"
<<y<<"=\n"<<f<<"\n";
WKX::MemoryMap<double> d(y);
cout<<"sizeof(WKX::MemoryMap<float>)="<<sizeof(d)<<"\n"
<<static_cast<double>(y)<<"=\n"<<d<<"\n";
return 0;
}
//结果:
00110000
01100001
0a 1 4 00000000 00110001 01000001 00000000
sizeof(WKX::MemoryMap<int>)=4
5=
00000101 00000000 00000000 00000000
-5=
11111011 11111111 11111111 11111111
sizeof(WKX::MemoryMap<float>)=4
0.1=
11001101 11001100 11001100 00111101
sizeof(WKX::MemoryMap<float>)=8
0.1=
00000000 00000000 00000000 10100000 10011001 10011001 10111001 00111111
wukexin 2009-12-15
  • 打赏
  • 举报
回复
谢谢各位大拿,我都迷惑了好几天了,原来关键问题是处在 MemoryMap <T>* ptr=&x.data_;
这里。
太乙 2009-12-14
  • 打赏
  • 举报
回复


#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 ;
template <typename T>
std::ostream& operator << (std::ostream& out,MemoryMap <T>& x) ;
template <typename T>
class MemoryMap
{
public:
MemoryMap(const T& x):data_(x) {};
protected:
friend std::ostream& operator << (std::ostream& out, MemoryMap <T>& x)
{
size_t length=sizeof(x.data_);
Byte* p=reinterpret_cast <Byte*>(&x.data_);
for (size_t i=0;i <length;i++)
{
out <<*p++ <<' ';
if (i>4 && i%4==0 ) out <<'\n';
}
return out;

}
private:
T data_;
};

int main()
{
std::string s="\00a";
cout << s << endl;
MemoryMap <std::string> mm(s);
cout << mm <<endl;
return 0;


}




likee003 2009-12-14
  • 打赏
  • 举报
回复
1、请问楼主有何问题?
2、判断语句不能够用赋值符号:
if (i>4 && i%4=0 ) out <<'\n';

lsldd 2009-12-14
  • 打赏
  • 举报
回复
MemoryMap <T>* ptr=&x.data_;
这里要强转。

if (i>4 && i%4=0 ) out <<'\n';
-->if (i>4 && i%4==0 ) out <<'\n';
baihacker 2009-12-14
  • 打赏
  • 举报
回复
#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 ;
template <typename T>
std::ostream& operator << (std::ostream& out,MemoryMap <T>& x) ;

template <typename T>
class MemoryMap
{
public:
MemoryMap(const T& x):data_(x) {};
protected:
friend std::ostream& operator << <T>(std::ostream& out, MemoryMap <T>& x);
private:
T data_;
};
//
template <typename T>
std::ostream& operator << (std::ostream& out,MemoryMap <T>& x)
{
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;
}
WaistCoat12 2009-12-14
  • 打赏
  • 举报
回复
啥问题啊?

64,678

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧