为了提高快速编写代码,可以采用VS2019片段设置生成get/set方法。
这里采用的宏定义的方法
参考链接:(点击下方卡片,了解更多信息)我个人实现的C++之get和set方法,使用宏定义_libaineu2004的博客-CSDN博客_c++ get set宏https://blog.csdn.net/libaineu2004/article/details/92433479
目录
1.找到代码片段管理器
2.找到模板,并创建一个新文件
比如:我要创建生成C++的get/set方法,我们可以复制其中一个文件,并粘贴到此处,重命名为cpgs,文件如下:
接下来,将文件权限修改成可读可写状态
这是我们要将改文件修改可写状态
3.在文件中,写入代码片段
在刚才创建的cpgs.snippet文件中填入信息。【下面是改文件全部代码】
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>cpgs</Title> <Shortcut>cpgs</Shortcut> <Description>C++的调用宏定义生成get/set函数</Description> <Author>Star Dream</Author> <SnippetTypes> <SnippetType>Expansion</SnippetType> <SnippetType>SurroundsWith</SnippetType> </SnippetTypes> </Header> <Snippet> <Code Language="cpp"><![CDATA[//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;\ }\ ]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
解析:
4.使用代码片段
打开你的Visual Studio程序,创建新项目,在窗口中输入cpgs即可
编辑器自动填入代码片段
创建一个类
接着对类重命名
重命名后,点击预览,再点击应用
完善类 :创建类的属性
引入头文件(这也是一个代码片段,需要另外添加)
温馨提示:读者可以手动敲代码
5.使用宏定义生成get/set函数
有了三个宏定义该怎么使用?
- PropertyBuilder_ReadWrite
- PropertyBuilder_ReadOnly
- PropertyBuilder_WriteOnly
添加上两行代码
PropertyBuilder_ReadWrite(int,i,Id,public); PropertyBuilder_ReadWrite(string, s, Name, public);
参数说明:
- 第一个参数:填入属性的变量类型
- 第二个参数:填入变量类型的缩写,一般就是开头的第1个字母
- 第三个参数:填入函数名(一般填写首字母大写的属性名)
- 第四个参数:填入权限修饰符,如public、protected、private
6.主函数调用
在调用函数时,我们可以看到宏定义已经帮我们创建好get/set函数了
参考以下代码(主函数)
int main() { //1.创建对象 Person p; //2.调用get/set函数 p.setId(1); p.setName("张三"); //3.打印输出 cout << "ID:" << p.getId() << "\t姓名:" << p.getName() << endl; return EXIT_SUCCESS; }
运行结果:
7.全部代码
//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;\
}\
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class Person
{
public:
Person();
~Person();
PropertyBuilder_ReadWrite(int, i, Id, public);
PropertyBuilder_ReadWrite(string, s, Name, public);
private:
int id;
string name;
};
Person::Person()
{
}
Person::~Person()
{
}
int main() {
//1.创建对象
Person p;
//2.调用get/set函数
p.setId(1);
p.setName("张三");
//3.打印输出
cout << "ID:" << p.getId() << "\t姓名:" << p.getName() << endl;
return EXIT_SUCCESS;
}