c++
复制代码
#include <iostream>
#include <string>
#define DECLARE_SETTER(type, name) \
void set_##name(const type &name) \
{ \
m_##name = name; \
}
#define DECLARE_GETTER(type, name) \
type name() const \
{ \
return m_##name; \
}
#define DECLARE_VARIABLE(type, name) \
type m_##name;
#define DECLARE_CLASS(className) \
class className \
{ \
public: \
DECLARE_SETTER(std::string, name) \
DECLARE_GETTER(std::string, name) \
\
DECLARE_SETTER(int, age) \
DECLARE_GETTER(int, age) \
\
private: \
DECLARE_VARIABLE(std::string, name) \
DECLARE_VARIABLE(int, age) \
};
#define TO_STRING(str) #str
int main()
{
std::string str = TO_STRING(123);
std::cout << str << std::endl;
str = TO_STRING(Hello World);
std::cout << str << std::endl;
DECLARE_CLASS(Person)
Person person;
person.set_name("tom");
person.set_age(23);
std::cout << person.name() << " " << person.age() << std::endl;
return EXIT_SUCCESS;
}