免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1705 | 回复: 5
打印 上一主题 下一主题

为什么指针输出的是一些数字 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-16 16:45 |只看该作者 |倒序浏览

  1.        
  2. CPhone类定义

  3. #include <string>
  4. #include <windows.h>
  5. #pragma   comment(lib, "Ws2_32.lib ")

  6. #define wchar_t WCHAR

  7. class CPhone
  8. {
  9. public:
  10.         //构造函数列表
  11.         CPhone();
  12.         CPhone(const WCHAR *pBrand, const unsigned int price, const char *phoneNumber, const bool isSup3G);
  13.         CPhone(const CPhone& phone);
  14.                
  15.         //析构函数
  16.         ~CPhone();
  17.                
  18.         //重载复制操作符
  19.         CPhone& operator =(const CPhone& phone);
  20.                
  21.         //类操作函数,基本上都是复制构造函数
  22.         WCHAR* getPhoneBrand(const CPhone& phone);        //获得电话的品牌
  23.         int        getPhonePrice(const CPhone& phone);        //获得电话的价格
  24.         char* getPhoneNumber(const CPhone& phone);        //获得电话号码
  25.         bool isSupport3G(const CPhone& phone);                //查询是否支持3G
  26.         char* changePhoneNumber(char *newPhoneNumber);//更改电话号码,该函数调用后,必须显式释放申请的内存
  27.         void displayPhoneBrand(void); //显示电话品牌
  28.         void displayPhonePrice(void); //显示电话价格
  29.         void displayPhoneNumber(void);//显示电话号码
  30.                
  31. private:
  32.         WCHAR                                *m_pBrand;                                                        //电话品牌
  33.         unsigned int                m_price;                                                        //电话价格
  34.         char                                *m_phoneNumber;                                                //电话号码
  35.         bool                                m_isSupport3G;                                                //是否支持3G

  36. };


复制代码
  1. CPhone类成员的实现


  2. //#include <stdio.h>
  3. #include <iostream>
  4. #include "stdafx.h"
  5. #include <wininet.h>
  6. #include <assert.h>

  7. #include "CPhone.h"



  8. CPhone::CPhone() : m_pBrand(NULL), m_price(0), m_phoneNumber(NULL), m_isSupport3G(FALSE) {}

  9. CPhone::CPhone(const WCHAR *pBrand, const unsigned int price, const char *phoneNumber, const bool isSup3G)
  10. {
  11.         assert(NULL!=pBrand);
  12.         assert(NULL!=phoneNumber);

  13.         this->m_pBrand = new WCHAR(wcslen(pBrand)+1);
  14.         this->m_phoneNumber = new char(strlen(phoneNumber)+1);
  15.                
  16.         if ((NULL!=this->m_pBrand) && (NULL!=this->m_phoneNumber))
  17.         {
  18.                 wcscpy(this->m_pBrand, pBrand);
  19.                 this->m_price = price;
  20.                 strcpy(this->m_phoneNumber, phoneNumber);
  21.                 this->m_isSupport3G = isSup3G;
  22.         }
  23. }

  24. CPhone::CPhone(const CPhone& phone)
  25. {
  26.         assert(NULL!=phone.m_pBrand);
  27.         assert(NULL!=phone.m_phoneNumber);

  28.         this->m_pBrand = new WCHAR(wcslen(phone.m_pBrand)+1);
  29.         this->m_phoneNumber = new char(strlen(phone.m_phoneNumber)+1);

  30.         if ((NULL!=this->m_pBrand) && (NULL!=this->m_phoneNumber))
  31.         {
  32.                 wcscpy(this->m_pBrand, phone.m_pBrand);
  33.                 this->m_price = phone.m_price;
  34.                 strcpy(this->m_phoneNumber, phone.m_phoneNumber);
  35.                 this->m_isSupport3G = phone.m_isSupport3G;
  36.         }
  37. }

  38. CPhone& CPhone::operator =(const CPhone& phone)
  39. {
  40.         if (this == &phone)
  41.         {
  42.                 return *this;
  43.         }
  44.        
  45.         delete this->m_pBrand;
  46.         delete this->m_phoneNumber;

  47.         assert(phone.m_pBrand);
  48.         assert(phone.m_phoneNumber);

  49.         this->m_pBrand = new WCHAR(wcslen(phone.m_pBrand)+1);
  50.         this->m_phoneNumber = new char(strlen(phone.m_phoneNumber)+1);
  51.                
  52.         if ((NULL==this->m_pBrand) || (NULL==this->m_phoneNumber))
  53.         {
  54.                 exit(1);
  55.         }
  56.        
  57.         wcscpy(this->m_pBrand, phone.m_pBrand);
  58.         this->m_price = phone.m_price;
  59.         strcpy(this->m_phoneNumber, phone.m_phoneNumber);
  60.         this->m_isSupport3G = phone.m_isSupport3G;
  61.        
  62.         return *this;

  63. }

  64. CPhone::~CPhone()
  65. {
  66.         delete        m_pBrand;
  67.         delete        m_phoneNumber;

  68.         m_pBrand = NULL;
  69.         m_phoneNumber = NULL;
  70. }

  71. /***************************
  72. 函数目的:返回CPhone对象的品牌
  73. 参数:CPhone对象的const引用
  74. 返回值:指向Cphone对象品牌字符串的首指针
  75. ***************************/
  76. WCHAR* CPhone::getPhoneBrand(const CPhone& phone)
  77. {
  78.         if (NULL==phone.m_pBrand)
  79.         {
  80.                 return NULL;
  81.         }
  82.                
  83.                 //函数返回电话品牌字符数组的头指针,调用时可以用数组保存该字符串然后输出
  84.         return phone.m_pBrand;
  85. }
  86.        
  87. int        CPhone::getPhonePrice(const CPhone& phone)
  88. {
  89.         if (0==phone.m_price)
  90.         {
  91.                 return 0;
  92.         }

  93.         return phone.m_price;
  94. }

  95. char* CPhone::getPhoneNumber(const CPhone& phone)
  96. {
  97.         if (NULL==phone.m_phoneNumber)
  98.         {
  99.                 return NULL;
  100.         }
  101.                
  102.                 //该函数使用方法与getPhoneBrand()一致
  103.         return phone.m_phoneNumber;
  104. }

  105. bool CPhone::isSupport3G(const CPhone& phone)
  106. {
  107.         return phone.m_isSupport3G;
  108. }

  109. char* CPhone::changePhoneNumber(char *newPhoneNumber)
  110. {
  111.         assert(NULL!=newPhoneNumber);

  112.         if (strlen(this->m_phoneNumber) < strlen(newPhoneNumber))
  113.         {
  114.                 std::cout<<"Error";
  115.                 return NULL;
  116.         }

  117.         strcpy(this->m_phoneNumber, newPhoneNumber);
  118.         return this->m_phoneNumber;
  119. }

  120. void CPhone::displayPhoneBrand(void)
  121. {
  122.         if (NULL==this->m_pBrand)
  123.         {
  124.                 return ;
  125.         }

  126.         setlocale(LC_ALL, "");
  127.         //setlocale(LC_ALL, NULL);
  128.         //WCHAR *p = this->m_pBrand;

  129.         while (*this->m_pBrand!='\0')
  130.         {
  131.                 //wprintf(L"%S\n ", *this->m_pBrand);
  132.                 std::wcout<<*this->m_pBrand;
  133.                 this->m_pBrand++;
  134.         }
  135. }

  136. void CPhone::displayPhonePrice(void)
  137. {
  138.         if (0==this->m_price)
  139.         {
  140.                 return ;
  141.         }

  142.         std::cout<<this->m_price;
  143. }

  144. void CPhone::displayPhoneNumber(void)
  145. {
  146.         if (NULL==this->m_phoneNumber)
  147.         {
  148.                 return ;
  149.         }
  150.                
  151.         //char *p = this->m_phoneNumber;
  152.         while (*this->m_phoneNumber!='\0')
  153.         {
  154.                 std::cout<<*this->m_phoneNumber;
  155.                 this->m_phoneNumber++;
  156.         }
  157. }
复制代码
  1. main函数

  2. #include "CPhone.h"
  3. #include <iostream>
  4. #include <string>


  5. using namespace std;

  6. int main()
  7. {
  8.         //setlocale(LC_ALL, "");
  9.         WCHAR arry[] = L"诺基亚";
  10.         CPhone nokia(arry, 1500, "13030204020", FALSE);
  11.         nokia.displayPhoneBrand();
  12.         cout<<endl;
  13.         nokia.displayPhoneNumber();
  14.         cout<<endl;
  15.         nokia.displayPhonePrice();

  16.         getchar();
  17.         return 0;
  18. }
复制代码
为什么nokia.displayPhoneBrand()输出的结果是358342252220122

论坛徽章:
0
2 [报告]
发表于 2012-01-16 20:55 |只看该作者
CU的大牛们帮帮小弟,跪谢了

论坛徽章:
0
3 [报告]
发表于 2012-01-16 21:40 |只看该作者
直接std::wcout<<this->m_pBrand;不就行了。
你还this->m_pBrand++,如果再display一次不就没有了。

另外有几个成员函数的参数是本对象的引用,感觉很奇怪,又不是类函数。
Cphone a,b;
a.isSupport3G(b);

论坛徽章:
0
4 [报告]
发表于 2012-01-17 12:38 |只看该作者
jetking 发表于 2012-01-16 21:40
直接std::wcoutm_pBrand++,如果再display一次不就没有了。

另外有几个成员函数的参数是本对象的引用, ...


我目的是想输出m_pBrand所指向的字符串

那几个成员函数是小弟想学习C++类,练手用的

论坛徽章:
0
5 [报告]
发表于 2012-01-17 12:56 |只看该作者
探索下 WCHAR 定义的是什么?是不是真的字符类型,还是 short之类?

论坛徽章:
0
6 [报告]
发表于 2012-01-17 13:53 |只看该作者
#define wchar_t WCHAR
我这个VC6不知道是什么回事,引入了头文件还是识别不了WCHAR,可以肯定WCHAR是字符类型
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP