将变量名变为字符串

将变量名 变为字符串的方法,很神奇,今天刚发现

#include <iostream>
using namespace std;
#define _showMem(str,v) showMem(#str,v)
struct student
{
    string name;
    int age;
    char sex;
};
void showMem(const char * str,int v)
{
    cout<<str<<"  "<<v<<endl;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!
";
    student s1;
    s1.name = "lili";
    s1.age = 23;
    s1.sex = 'f';
    _showMem(student.age,s1.age);
    system("pause");
    return 0;
}

输出

原文地址:https://www.cnblogs.com/techdreaming/p/5693338.html