c++自定义类型的字面值常量

int i = 888;

编译器会将数字888转成一个int对象,对于自定义的类型也可以这么做:

#define debug qDebug()<<
struct ceshi
{
    int frist;
    int second;
    ceshi(int one = 0,int two = 0):frist{one},second{two}
    {
    }
};
ceshi operator"" _xxx(long double f)
{
    return ceshi(f,88);
}

int main(int argc, char *argv[])
{
    ceshi f = 44.4_xxx;
    debug f.frist << f.second;
}

操作符""后面以下划线开始的符号作为自定义类型字面值常量运算符。必须以下划线开头,非下划线开头的作为标准库保留使用的,支持以下几种参数的:

operator "" identifier (const char *);
operator "" identifier (unsigned long long int);
operator "" identifier (long double);
operator "" identifier (char);
operator "" identifier (const char*, std::size_t);

猜你喜欢

转载自blog.csdn.net/kenfan1647/article/details/114443456