类的构造函数 类型一样 变量名不一样无所谓

2021/12/30 6:37:16

本文主要是介绍类的构造函数 类型一样 变量名不一样无所谓,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

// 你必须定义一个 `main()` 函数入口。
#include <iostream>
using namespace std;
#include <string.h>
typedef const char* FX_LPCSTR;
typedef char FX_CHAR;
typedef int FX_STRSIZE;
typedef int FX_BOOL;
typedef unsigned char FX_BYTE;
 
#define FX_Alloc(type, size)						(type*)calloc(size, sizeof(type))
#define FX_Free(ptr)								free(ptr)
struct CFX_StringData {
    long		m_nRefs;
    FX_STRSIZE	m_nDataLength;
    FX_STRSIZE	m_nAllocLength;
    FX_CHAR		m_String[1];
};
static CFX_StringData* FX_AllocString(int nLen)
{
    // |nLen| is currently declared as in |int|. TODO(palmer): It should be
    // a |size_t|, or at least unsigned.
 if (nLen == 0 || nLen < 0) {
        return NULL;
    }
    int nSize = nLen;
    nSize += sizeof(long) * 3 + 1;
    CFX_StringData* pData = (CFX_StringData*)FX_Alloc(FX_BYTE, nSize);
    if (!pData) {
        return NULL;
    }
    pData->m_nAllocLength = nLen;
    pData->m_nDataLength = nLen;
    pData->m_nRefs = 1;
    pData->m_String[nLen] = 0;
    return pData;
}
 static void FX_ReleaseString(CFX_StringData* pData)
{
    if (pData == NULL) {
        return;
    }
    pData->m_nRefs --;
    if (pData->m_nRefs <= 0) {
        FX_Free(pData);
    }
}




class CFX_Object
{
  public:
  void* operator new(size_t size, FX_LPCSTR file, int line)
  {
    return malloc(size);
  }
   
  void operator delete(void*p,FX_LPCSTR file,int size)
  {
    free(p);
  }
  void* operator new(size_t size)
  {
    return malloc(size);
  }
  void operator delete(void* p)
  {
    free(p);
  }
    void*			operator new[] (size_t size, FX_LPCSTR file, int line)
    {
        return malloc(size);
    }
    void			operator delete[] (void* p, FX_LPCSTR file, int line)
    {
        free(p);
    }
    void*			operator new[] (size_t size)
    {
        return malloc(size);
    }
    void			operator delete[] (void* p)
    {
        free(p);
    }
    void*			operator new (size_t, void* buf)
    {
        return buf;
    }
    void			operator delete (void*, void*)							{}
};
 
 
class CFX_ByteString : public CFX_Object
{
public:
   CFX_ByteString(FX_LPCSTR ptr, FX_STRSIZE len = -1);
protected:
    struct CFX_StringData*	m_pData;
    void					AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const;
};
 
CFX_ByteString::CFX_ByteString(FX_LPCSTR lpsz, FX_STRSIZE nLen)
{
    if (nLen < 0) {
        nLen = lpsz ? (FX_STRSIZE)strlen(lpsz) : 0;
    }
    if (nLen) {
        m_pData = FX_AllocString(nLen);
        if (m_pData) {
            memcpy(m_pData->m_String, lpsz, nLen);
        }
    } else {
        m_pData = NULL;
    }
}
 

void CFX_ByteString::AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const
{
  return;
}

int main()
{
 
  cout << sizeof(CFX_StringData) << endl;
  return 0;
}
 
		

  



这篇关于类的构造函数 类型一样 变量名不一样无所谓的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程