一、自己的方法
本人在C++项目中经常需要用到get和set方法,但是c++并不像 java的eclipse有自动生成 get 和 set 方法。如果手写是可以,但是当属性特别多的时候会非常麻烦。。。于是决定使用宏定义方法来解决。
本人在参考了网上资料(参见文章末尾的参考文献)之后,实现了自己的版本,cplusplusgetset.h头文件如下:
#pragma once
//PropertyBuilderByTypeName 用于生成类的成员变量
//并生成set和get方法
//variable_type为变量类型,可以是指针类型,也可以是非指针类型,例如int,int*等
//type_shortname为变量类型的缩写,例如bool缩写为b,int缩写为i,double缩写为d等
//method_name为方法名称
//access_permission为变量的访问权限(public, protected, private)
#define PropertyBuilder_ReadWrite(variable_type, type_shortname, method_name, access_permission)\
access_permission:\
variable_type m_##type_shortname##method_name;\
public:\
inline variable_type get##method_name(void)\
{\
return m_##type_shortname##method_name;\
}\
inline void set##method_name(variable_type v)\
{\
m_##type_shortname##method_name = v;\
}\
#define PropertyBuilder_ReadOnly(variable_type, type_shortname, method_name, access_permission)\
access_permission:\
variable_type m_##type_shortname##method_name;\
public:\
inline variable_type get##method_name(void) const\
{\
return m_##type_shortname##method_name;\
}\
#define PropertyBuilder_WriteOnly(variable_type, type_shortname, method_name, access_permission)\
access_permission:\
variable_type m_##type_shortname##method_name;\
public:\
inline void set##method_name(variable_type v)\
{\
m_##type_shortname##method_name = v;\
}\
使用说明:
class MyClass
{
public:
MyClass()
{
m_bLookAhead = true;
m_dStatus = NULL;
m_iHello = 0;
}
~MyClass() {}
PropertyBuilder_ReadWrite(bool, b, LookAhead, protected)//bool m_bLookAhead;
PropertyBuilder_ReadWrite(double*, d, Status, protected)//bool* m_pStatus;
PropertyBuilder_WriteOnly(int, i, Hello, private)//int m_iHello;
public:
void test()
{
setLookAhead(true);
double a = 0;
setStatus(&a);
setHello(5);
bool r = getLookAhead();
}
};
二、VTK的方法
\VTK-9.1.0\include\vtk-9.1\vtkSetGet.h
vtkSetGet.h
//
// Set built-in type. Creates member Set"name"() (e.g., SetVisibility());
//
#define vtkSetMacro(name, type) \
virtual void Set##name(type _arg) \
{ \
vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting " #name " to " << _arg); \
if (this->name != _arg) \
{ \
this->name = _arg; \
this->Modified(); \
} \
}
//
// Get built-in type. Creates member Get"name"() (e.g., GetVisibility());
//
#define vtkGetMacro(name, type) \
virtual type Get##name() \
{ \
vtkDebugMacro(<< this->GetClassName() << " (" << this << "): returning " << #name " of " \
<< this->name); \
return this->name; \
}
//
// Set 'enum class' type. Creates member Set"name"() (e.g., SetKind());
// vtkSetMacro can't be used because 'enum class' won't trivially convert to integer for logging.
//
#define vtkSetEnumMacro(name, enumType) \
virtual void Set##name(enumType _arg) \
{ \
vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting " #name " to " \
<< static_cast<std::underlying_type<enumType>::type>(_arg)); \
if (this->name != _arg) \
{ \
this->name = _arg; \
this->Modified(); \
} \
}
//
// Get 'enum class' type. Creates member Get"name"() (e.g., GetKind());
// vtkSetMacro can't be used because 'enum class' won't trivially convert to integer for logging.
//
#define vtkGetEnumMacro(name, enumType) \
virtual enumType Get##name() const \
{ \
vtkDebugMacro(<< this->GetClassName() << " (" << this << "): returning " << #name " of " \
<< static_cast<std::underlying_type<enumType>::type>(this->name)); \
return this->name; \
}
//
// Set character string. Creates member Set"name"()
// (e.g., SetFilename(char *));
//
#define vtkSetStringMacro(name) \
virtual void Set##name(const char* _arg) vtkSetStringBodyMacro(name, _arg)
// Set a file path, like vtkSetStringMacro but with VTK_FILEPATH hint.
#define vtkSetFilePathMacro(name) \
virtual void Set##name(VTK_FILEPATH const char* _arg) vtkSetStringBodyMacro(name, _arg)
---
参考文献