当前位置: 首页 > news >正文

网站建设新媒体运营网站的后台

网站建设新媒体运营,网站的后台,深圳网站优化包年,视频发布网站有哪些内容Property在C中的实现与应用 以下是在C中实现属性(Property)的完整实现方案&#xff0c;结合模板技术和运算符重载实现类型安全的属性访问&#xff0c;支持独立模块化封装&#xff1a; #include <iostream> #include <functional>template<typename HostType, t…

Property在C++中的实现与应用

以下是在C++中实现属性(Property)的完整实现方案,结合模板技术和运算符重载实现类型安全的属性访问,支持独立模块化封装:

#include <iostream>
#include <functional>template<typename HostType, typename ValueType>
class Property {using Getter = ValueType (HostType::*)() const;using Setter = void (HostType::*)(const ValueType&);HostType* host;Getter getter;Setter setter;public:Property(HostType* h, Getter g, Setter s = nullptr): host(h), getter(g), setter(s) {}// 读取属性值运算符重载 operator ValueType() const {if (!getter) throw std::logic_error("Getter not defined");return (host->*getter)();}// 赋值运算符重载 ValueType operator=(const ValueType& value) {if (!setter) throw std::logic_error("Setter not defined");(host->*setter)(value);return value;}// 只读属性特化 class ReadOnly {Getter getter;const HostType* host;public:ReadOnly(const HostType* h, Getter g) : host(h), getter(g) {}operator ValueType() const { return (host->*getter)(); }};// 重载 << 运算符friend std::ostream& operator<<(std::ostream& os, const Property& prop) {os << static_cast<ValueType>(prop);return os;}// 重载 << 运算符(只读属性)friend std::ostream& operator<<(std::ostream& os, const ReadOnly& prop) {os << static_cast<ValueType>(prop);return os;}
};
#include "Property.h"/* 使用示例 */
class User {std::string name_;int age_;// 私有访问方法 std::string get_name() const { return name_; }void set_name(const std::string& val) { name_ = val; }int get_age() const { return age_; }public:// 公开属性定义 Property<User, std::string> Name{this, &User::get_name,&User::set_name };Property<User, int>::ReadOnly Age{this,&User::get_age };User(const std::string& name, int age) : name_(name), age_(age) {}
};int main() {User user("zhengtianzuo", 18);// 属性赋值 user.Name = "zhengtianzuo";// 属性读取 std::cout << "Name: " << user.Name << std::endl;std::cout << "Age: " << user.Age << std::endl;// user.Age = 20; // 编译错误(只读属性)return 0;
}

实现原理分析

  • 模板类设计:
    Property模板类封装宿主对象指针、getter/setter方法指针
    通过HostType模板参数实现与宿主类的解耦
    使用嵌套类ReadOnly实现只读属性特化
  • 运算符重载:
    operator ValueType()实现属性值的隐式转换(读取操作)
    operator=重载实现属性赋值操作
    通过函数指针调用实际存取方法
  • 类型安全检查:
    getter/setter必须匹配宿主类和值类型
    只读属性禁用赋值运算符(编译期检查)

关键特性

  • 访问控制:
    通过setter存在性判断实现读写/只读控制
    支持自定义存取逻辑(如数据校验)
  • 性能优化:
    零运行时开销(编译期绑定)
    不依赖虚函数或动态类型检查
  • 模块独立性:
    属性实现与业务逻辑完全解耦
    可复用至任意需要属性特征的类

扩展应用场景

// 数据校验示例 
class Temperature {double celsius_;double get_c() const { return celsius_; }void set_c(double val) { if(val < -273.15) throw std::range_error("Below absolute zero");celsius_ = val;}public:Property<Temperature, double> Celsius{this, &Temperature::get_c, &Temperature::set_c};
};// 派生类支持 
class Animal {
protected:virtual std::string sound() const = 0;
};class Dog : public Animal {std::string sound() const override { return "Woof"; }
public:Property<Dog, std::string> Sound{this, &Dog::sound};
};

该实现方案完整实现了C++中的属性特征,通过模板技术和运算符重载提供了类型安全、高性能的属性访问机制,可作为独立模块集成到各类项目中。

完整代码

Github

作者郑天佐
邮箱zhengtianzuo06@163.com
主页http://www.zhengtianzuo.com
githubhttps://github.com/zhengtianzuo
http://www.dtcms.com/a/583287.html

相关文章:

  • 自媒体可做外链网站彩票网站建设制作
  • 北京市建设工程发包承包交易中心网站煤炭网站建设企业
  • 数字人企业全链条技术解析
  • 做好网站维护管理海南人才网
  • 商贸办公网站入口福建高端建设网站
  • 大型网站建设公司制作网站一键生成网站的软件
  • 04. STM32单片机驱动和控制彩屏_显示JPG_刷新测试_指针仪表方法
  • 笔试强训:Week-4
  • 网站建设投票系统总结二级目录网站怎么做
  • 【C++】:C++聊天室后台服务器之Spdlog日志组件安装与使用
  • 青岛建设工程信息网站如何做网站步骤
  • shell(2)--变量、算数运算、测试表达式、常用的操作运算符、文件测试运算符、整数值比较
  • php怎样做网站管理后台设计网站软件开发
  • 有没有和小孩做的网站新闻发布会邀请哪些媒体
  • 浙江做铁塔的公司网站网站年费如何做会计分录
  • 开州网站建设网站项目有需要什么技术支持
  • Linux27 线程同步--条件变量
  • seo网站优化推广怎么做盐城网站建设有限公司
  • Numpy一维、二维、三维数组切片实例
  • 手机端网站建站云南网站建设招商
  • 开放获取 SuperMamba 小目标检测特征增强框架
  • 布吉网站建设哪家技术好怎样在手机上创建网站
  • seo是什么?seo网站关键词优化哪家好
  • MinimalWalls v1.9.8 | 提供高质量简约壁纸,支持一键下载、收藏和自动更换等功能,界面干净并支持深浅模式切换
  • 网站优化推广seo公司网站建设的有什么需求
  • 成都网站建设与网站制作网站建设的技术要求
  • 东营住房与城乡建设部网站网站优化 北京
  • asp网站免费模板专门做外链的网站
  • 网站开发技术课程设计说明书做卖挖掘机的网站
  • 购物网站功能模块图wordpress 中英文站点