码迷,mamicode.com
首页 > 其他好文 > 详细

自定义字符类

时间:2018-06-13 14:52:12      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:字符   ring   ons   使用   长度   const   char   MF   返回   

   当 VC不使用MFC,无法使用属于MFC的CString,为此自定义一个,先暂时使用,后续完善。

头文件:

#pragma once

#define MAX_LOADSTRING 100 // 最大字符数

class CString
{
  public:
      
      char *c_str; 
      WCHAR *w_str;

      void operator = (const char *str); // = 操作,获得c字符串: str = "sss"; 式样
      void operator + (const CString str); // + 操作,获得s字符串加: s1 = s2 + s3; 式样
      void operator += (const CString str); // += 操作,获得s字符串加: s1 += "s2"; 式样

      int StrCount(void);// 返回字符串长度s.StrCount()
  
      CString(void);

  private:

      char cSAr[MAX_LOADSTRING];
      WCHAR wSAr[MAX_LOADSTRING];

      int iStrCount; // 字符串数量
};

C文件:

#include "stdafx.h"
#include "String.h"

CString::CString(void)
{
    c_str = cSAr;
    w_str = wSAr;
}

void CString::operator = (const char *str) // =
{
    int i;

    if(str == NULL) return;
    iStrCount = strlen(str);//获取字符串长度
    for(i = 0; i < iStrCount; i++)
       {
          cSAr[i] = str[i];
          wSAr[i] = str[i];
       }
    cSAr[i] = 0;
    wSAr[i] = 0;
}

void CString::operator + (const CString str) // +
{
     //
}

void CString::operator += (const CString str) // +=
{
  //
}

int CString::StrCount(void)
{
    return iStrCount;
}

 

自定义字符类

标签:字符   ring   ons   使用   长度   const   char   MF   返回   

原文地址:https://www.cnblogs.com/hbg200/p/9176945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!