windows下 C++ 实现类属性的get和set方式

目录

通用的方式

Windows平台方式:

属性 (C++)

注意:

 用法:

使用类模板的方式: 

 用法:

实验代码: 

结果:

自己的想法:


通用的方式

C#的语法中自带类属性的get和set方式,可以很优雅的读写属性.在C++中要是向使用,通俗的写法是写内联函数,{C++在类的内部实现的函数默认为内联函数}

例如:

class MyClass
{
public:
	int GetValue() { return _value; }
	void SetValue(int num) { _value = num; }
	string GetStrValue() { return _strValue; }
	void SetValue(string str) { _strValue = str; }
private:
	int _value;
	string _strValue;
};

这是最简单的,也是最通用的不区分平台.

Windows平台方式:

在Windows系统下使用VS IED有种方式可以实现和C#属性的用法类似的方式:

属性 (C++) | Microsoft Docs 微软的文档

属性 (C++)

Microsoft 专用

此特性可应用于类或结构定义中的非静态“虚拟数据成员”。 编译器通过将这些“虚拟数据成员”的引用更改为函数调用来将其作为数据成员处理。

注意:

第一个类是普通的类,只要在通用的方式添加两行代码就可以实现

	__declspec(property(get = GetValue, put = SetValue)) int value;
	__declspec(property(get = GetStrValue, put = SetValue)) string strValue;

这行语法的内容是,只有 __declspec(property(get = GetValue, put = SetValue)) int value;

红色的你自己写的内联函数,紫色的是定义的属性,也就是你读写的时候的值;

 用法:

	MyClass myc;
	myc.value = 10;
	myc.strValue = "hello world";
	cout << myc.value << endl;
	cout << myc.strValue << endl;

使用类模板的方式: 

template <typename T>
class MyTempClass
{
public:
	T GetValue() { return _value; }
	void SetValue(T num) { _value = num; }
	__declspec(property(get = GetValue, put = SetValue)) T value;
private:
	T _value;
};

 用法:

	MyTempClass<int> Mytc;
	Mytc.value = 20;
	cout << Mytc.value << endl;
	MyTempClass<string> strMytc;
	strMytc.value = "qwert";
	cout << strMytc.value << endl;
	MyTempClass<double> dMytc;
	dMytc.value = 3.1415;
	cout << dMytc.value << endl;

实验代码: 

// CppGetSet.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class MyClass
{
public:
	int GetValue() { return _value; }
	void SetValue(int num) { _value = num; }
	string GetStrValue() { return _strValue; }
	void SetValue(string str) { _strValue = str; }
	__declspec(property(get = GetValue, put = SetValue)) int value;
	__declspec(property(get = GetStrValue, put = SetValue)) string strValue;
private:
	int _value;
	string _strValue;
};

template <typename T>
class MyTempClass
{
public:
	T GetValue() { return _value; }
	void SetValue(T num) { _value = num; }
	__declspec(property(get = GetValue, put = SetValue)) T value;
private:
	T _value;
};

int main()
{
	MyClass myc;
	myc.value = 10;
	myc.strValue = "hello world";
	cout << myc.value << endl;
	cout << myc.strValue << endl;
	MyTempClass<int> Mytc;
	Mytc.value = 20;
	cout << Mytc.value << endl;
	MyTempClass<string> strMytc;
	strMytc.value = "qwert";
	cout << strMytc.value << endl;
	MyTempClass<double> dMytc;
	dMytc.value = 3.1415;
	cout << dMytc.value << endl;
    return 0;
}

结果:

自己的想法:

虽然说这种方式很好用,但是个人认为并不是很好.因为这个方法只适用于Windows平台,要是使用跨平台的时候是不可以的,若只是在Windows平台下用,这个方式很是很值得推荐的,但是要是在Linux平台下使用,可能会编译过不(没有试过,).

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是使用 Fast DDS 实现 set 和 get 方法通信的 C++ 示例代码: ```c++ #include <fastrtps/attributes/ParticipantAttributes.h> #include <fastrtps/attributes/PublisherAttributes.h> #include <fastrtps/attributes/SubscriberAttributes.h> #include <fastrtps/Domain.h> #include <fastrtps/publisher/Publisher.h> #include <fastrtps/subscriber/Subscriber.h> #include <fastrtps/xmlparser/XMLProfileManager.h> using namespace eprosima::fastrtps; // 定义数据型 struct MyDataType { int32_t id; std::string name; }; class MyReaderListener : public SubscriberListener { public: void onNewDataMessage(Subscriber *sub) override { MyDataType data; SampleInfo_t info; if (sub->takeNextData(&data, &info)) { std::cout << "Received data: " << data.name << std::endl; } } }; class MyWriter { public: MyWriter() { // 创建参与者 ParticipantAttributes participantAttrs; participantAttrs.rtps.builtin.domainId = 0; participantAttrs.rtps.builtin.leaseDuration = c_TimeInfinite; participantAttrs.rtps.setName("MyParticipant"); participant = Domain::createParticipant(participantAttrs); if (participant == nullptr) { std::cout << "Error creating participant" << std::endl; return; } // 创建发布者 PublisherAttributes publisherAttrs; publisherAttrs.topic.topicDataType = "MyDataType"; publisherAttrs.topic.topicName = "MyTopic"; publisher = Domain::createPublisher(participant, publisherAttrs, nullptr); if (publisher == nullptr) { std::cout << "Error creating publisher" << std::endl; return; } } void setData(const MyDataType& data) { // 发布数据 publisher->write(&data); } private: Participant *participant; Publisher *publisher; }; class MyReader { public: MyReader() { // 创建参与者 ParticipantAttributes participantAttrs; participantAttrs.rtps.builtin.domainId = 0; participantAttrs.rtps.builtin.leaseDuration = c_TimeInfinite; participantAttrs.rtps.setName("MyParticipant"); participant = Domain::createParticipant(participantAttrs); if (participant == nullptr) { std::cout << "Error creating participant" << std::endl; return; } // 创建订阅者 SubscriberAttributes subscriberAttrs; subscriberAttrs.topic.topicDataType = "MyDataType"; subscriberAttrs.topic.topicName = "MyTopic"; listener = new MyReaderListener(); subscriber = Domain::createSubscriber(participant, subscriberAttrs, listener); if (subscriber == nullptr) { std::cout << "Error creating subscriber" << std::endl; return; } } MyDataType getData() { // 从订阅者中获取数据 MyDataType data; SampleInfo_t info; if (subscriber->takeNextData(&data, &info)) { return data; } return {}; } private: Participant *participant; Subscriber *subscriber; MyReaderListener *listener; }; int main() { // 创建数据写入者和读取者 MyWriter writer; MyReader reader; // 设置数据并发布 MyDataType data; data.id = 1; data.name = "Alice"; writer.setData(data); // 获取数据并打印 MyDataType receivedData = reader.getData(); std::cout << "Received data: " << receivedData.name << std::endl; return 0; } ``` 以上代码演示了如何使用 Fast DDS 创建数据写入者和读取者,并通过发布/订阅模型实现数据的 set 和 get。在示例中,数据型为 `MyDataType`,包括一个 `id` 和一个 `name` 字段,数据写入者使用 `MyWriter` ,数据读取者使用 `MyReader` ,数据写入者发布数据,数据读取者从订阅者中获取数据,并打印接收到的数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

波雅_汉库克

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

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

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

打赏作者

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

抵扣说明:

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

余额充值