VS2019快速生成get/set方法

3 篇文章 0 订阅

为了提高快速编写代码,可以采用VS2019片段设置生成get/set方法。

这里采用的宏定义的方法

参考链接:点击下方卡片,了解更多信息我个人实现的C++之get和set方法,使用宏定义_libaineu2004的博客-CSDN博客_c++ get set宏https://blog.csdn.net/libaineu2004/article/details/92433479


目录

1.找到代码片段管理器

2.找到模板,并创建一个新文件

3.在文件中,写入代码片段

4.使用代码片段

5.使用宏定义生成get/set函数

6.主函数调用

7.全部代码 


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函数

有了三个宏定义该怎么使用?

  1. PropertyBuilder_ReadWrite
  2. PropertyBuilder_ReadOnly
  3. PropertyBuilder_WriteOnly

添加上两行代码

PropertyBuilder_ReadWrite(int,i,Id,public);
PropertyBuilder_ReadWrite(string, s, Name, public);

参数说明:

  1. 第一个参数:填入属性的变量类型
  2. 第二个参数:填入变量类型的缩写,一般就是开头的第1个字母
  3. 第三个参数:填入函数名(一般填写首字母大写的属性名)
  4. 第四个参数:填入权限修饰符,如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;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fy哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值