Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c++可变类型vartype #22

Open
coneo opened this issue Nov 1, 2015 · 0 comments
Open

c++可变类型vartype #22

coneo opened this issue Nov 1, 2015 · 0 comments
Labels

Comments

@coneo
Copy link
Owner

coneo commented Nov 1, 2015

C++是一种强类型语言,也就是说,每个变量必须是有一种确定的类型。如果不确定,编译器则会认为非法。即使是c++11的auto和decltype也都是推导出类型,而非一个非确定类型。这在某些地方使用起来不是很方便,比如数据库数据读取:数据库字段有多种类型(byte,int,unsigned int,string甚至date),在读取数据库时,我们可以通过一个vartype来保存读取的每个字段,然后根据我们的需要来读取。用例如下:

//逻辑中表的结构,对应到数据库表的结构
struct stTB
{
  byte flag;
  int age;
  unsigned int onlinetime;
  char name[MAX_SIZE];
};

//假设该数组是从数据库中表读取出来的数据
VarType values[datasize];

//以下是把数据库读取出来的数据保存到内存中
stTB tb;
tb.flag = values[0];
tb.age = values[1];
tb.onlinetime = values[2];
strncpy(tb.name, values[3], MAX_SIZE);

从以上代码看到,我们把数据库中字段的数据都保存到了以VarType类型的变量中,然后可以通过赋值的方式直接从VarType中获取出来。保证了接口的统一和简单性。VarType也可以用于其他类似的地方,比如读取配置(因为配置也是有多种类型的),这里就不举例子了。下面是VarType实现,代码并非完整,只包含了string和int,其他的byte,short,long等请自行扩展。
vartype.h:

#ifndef _VARTYPE_H
#define _VARTYPE_H

#include <string>

class VarType
{
public:
    VarType();
    VarType(const VarType& val);

    VarType& operator = (const VarType& var);

    VarType(const char* val);
    VarType(const std::string& val);
    VarType(int val);
    VarType(unsigned int val);
    VarType(long val);
    VarType(unsigned long val);

    operator const char* ();
    operator int ();
    operator unsigned int ();
    operator long ();
    operator unsigned long ();

private:
    std::string m_str;
};

#endif

vartype.cpp:

#include "vartype.h"

VarType::VarType()
{
    m_str.clear();
}

VarType::VarType(const VarType& val)
{
    this->m_str = val.m_str;
}

VarType& VarType::operator = (const VarType& var)
{
    if (&var != this)
    {
        this->m_str.clear();
        this->m_str = var.m_str;
    }
    return *this;
}

VarType::VarType(int val)
{
    m_str = std::to_string(val);
}

VarType::VarType(unsigned int val)
{
    m_str = std::to_string(val);
}

VarType::VarType(long val)
{
    m_str = std::to_string(val);
}

VarType::VarType(unsigned long val)
{
    m_str = std::to_string(val);
}

VarType::VarType(const char* val)
{
    m_str = val;
}
VarType::VarType(const std::string& val)
{
    m_str = val;
}

VarType::operator int ()
{
    return std::stoi(m_str);
}

VarType::operator unsigned int ()
{
    return static_cast<unsigned int>(std::stoul(m_str));
}

VarType::operator long ()
{
    return std::stol(m_str);
}

VarType::operator unsigned long ()
{
    return std::stoul(m_str);
}

VarType::operator const char* ()
{
    return m_str.c_str();
}

以下是一个测试例子:

#include "vartype.h"
#include <iostream>
using namespace std;

void foo(int val)
{
    cout << "foo:" << val << endl;
}

void test_int()
{
    //构造
    VarType var0(98);

    //拷贝构造
    VarType var1(var0);

    //赋值操作符
    VarType var2;
    var2 = var0;

    unsigned long num0 = 3000000000;
    VarType var3(num0);

    //隐式转换
    foo(var1);

    //隐式转换
    int i = var1;

    cout << "var0=" << (int)var0 << endl;
    cout << "var1=" << i << endl;
    cout << "var2=" << (int)var2 << endl;
    cout << "var3=" << (unsigned long)var3 << endl;
}
void test_str()
{
    VarType str0("str0");

    std::string tmpstr1 = "this is std string";
    VarType str1(tmpstr1);

    std::string tmpstr2 = (const char*)str1;
    //这里强转为string实际上是先转为const char*
    cout << "str0=" << (const char*)str0 << endl;
    cout << "str1=" << (std::string)str1 << endl;
    cout << "tmpstr2=" << tmpstr2 << endl;
}

int main(int argc, char**argv)
{
    //test_int();

    test_str();

    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant