How to generate random variable names in C++ using macros?

前端 未结 8 2023
别跟我提以往
别跟我提以往 2020-12-01 07:56

I\'m creating a macro in C++ that declares a variable and assigns some value to it. Depending on how the macro is used, the second occurrence of the macro can override the v

8条回答
  •  时光取名叫无心
    2020-12-01 08:51

    While I don't think its even possible, you should seriously consider making a class out of this.

    If you want a random element in a random array to hold a certain value, you can do this:

    std::vector< std::vector > m_vec;
    

    Then wrap it in a class, so the developer can only set a number:

    void set(int foo)
    {
        m_vec[random()][random()] = foo;
    }
    

    Is there any reason why you want it a macro? Random variable name sounds dangerous, what if it picks something already defined somewhere else in the code?

提交回复
热议问题