增量定义? [英] incremented define?

查看:25
本文介绍了增量定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何都要在每次使用时定义增量?

Is there anyways to have a define increment every time you use it?

例如

int a = ADEFINE;
int b = ADEFINE;

a 是 1,b 是 2.

a is 1 and b is 2.

推荐答案

如果你不需要编译时常量,你可以这样做来枚举类:

If you don't need compile-time-constants, you could do something like this to enumerate classes:

int counter() {
    static int i = 0;
    return i++;
}

template<class T>
int id() { 
    static int i = counter();
    return i; 
};

class A {};
class B {};

int main()
{
    std::cout << id<A>() << std::endl;
    std::cout << id<B>() << std::endl;
}

这篇关于增量定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆