IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 嵌入式linux学习笔记-- c++ 的 操作符重载 -> 正文阅读

[C++知识库]嵌入式linux学习笔记-- c++ 的 操作符重载

复习了一下c++ 的操作符重载以及 模板,随便写了一个demo 记录一下。

#include <iostream>
#define LOG(str) {printf("this is %s:%d \t%s\n",__FUNCTION__,__LINE__,str);}
class MyOptr final
{
private:
    uint16_t *m_ptr;
public:
    MyOptr();
    void init(uint16_t *ptr);
    MyOptr(uint16_t *ptr);

    MyOptr(const MyOptr& val);

    operator uint16_t();
    operator int16_t();
    operator int32_t();
    operator uint32_t();
    operator float();
    operator double();
    operator uint64_t();
    operator int64_t();
    
    template<typename T>
    operator T();

    MyOptr& operator=(uint16_t val);
    MyOptr& operator=(int16_t val);
    MyOptr& operator=(int32_t val);
    MyOptr& operator=(uint32_t val);
    MyOptr& operator=(uint64_t val);
    MyOptr& operator=(int64_t val);
    MyOptr& operator=(double val);
    MyOptr& operator=(float val);
    template<typename T>
    MyOptr& operator=(T val);

    MyOptr& operator<<(uint16_t val);
    MyOptr& operator<<(int16_t val);
    MyOptr& operator<<(int32_t val);
    MyOptr& operator<<(uint32_t val);
    MyOptr& operator<<(uint64_t val);
    MyOptr& operator<<(int64_t val);
    MyOptr& operator<<(double val);
    MyOptr& operator<<(float val);    
    template<typename T>
    MyOptr& operator<<(T val);   
    MyOptr operator[](unsigned int index) const;

};

typedef struct {
    int a;
    double b;
}my_str;
int main(void)
{
    MyOptr temp;
    temp = (int)123;
    temp = (double)123;
    temp = (float)123;
    temp = (int64_t)123;
    temp = (uint64_t)123;
    temp = (int32_t)123;
    temp = (int16_t)123;
    temp = (uint16_t)123;
    printf("--------------------------1-------\n");
    
    temp << (int)123;
    temp << (double)123;
    temp << (float)123;
    temp << (int64_t)123;
    temp << (uint64_t)123;
    temp << (int32_t)123;
    temp << (int16_t)123;
    temp << (uint16_t)123;
    
    printf("--------------------------2-------\n");
    {int      val = temp;}
    {double   val = temp;}
    {float    val = temp;}
    {int64_t  val = temp;}
    {uint64_t val = temp;}
    {int32_t  val = temp;}
    {int16_t  val = temp;}
    {uint16_t val = temp;}
    
    printf("--------------------------3-------\n");
    temp[123] =  (uint16_t)123;
    
    printf("--------------------------4-------\n");
    my_str m;
    temp << m;
    temp = m;
}

template<typename T>
MyOptr::operator T()
{
    LOG("template<typename T> MyOptr::operator T");
    return T{0};
}
template<typename T>
MyOptr& MyOptr::operator=(T val)
{
    LOG("template<typename T> MyOptr::operator=");
}

template<typename T>
MyOptr& MyOptr::operator<<(T val)
{
    LOG("template<typename T> MyOptr::operator<<");
}

MyOptr::MyOptr()
{
    LOG("MyOptr()");
}
void MyOptr::init(uint16_t *ptr)
{
    LOG("MyOptr::init(uint16_t *ptr)");
}
MyOptr::MyOptr(uint16_t *ptr)
{
    LOG("MyOptr(uint16_t *ptr)");
}
MyOptr::MyOptr(const MyOptr& val)
{
    LOG("MyOptr(const MyOptr& val)");
}
MyOptr::operator uint16_t()
{
    LOG("MyOptr::operator uint16_t()");
    return 0;
}
MyOptr::operator int16_t()
{
    LOG("MyOptr::operator int16_t()");
    return 0;
}
MyOptr::operator int32_t()
{
    LOG("MyOptr::operator int32_t()");
    return 0;
}
MyOptr::operator uint32_t()
{
    LOG("MyOptr::operator uint32_t()");
    return 0;
}
MyOptr::operator float()
{
    LOG("MyOptr::operator float()");
    return 0;
}
MyOptr::operator double()
{
    LOG("MyOptr::operator double()");
    return 0;
}
MyOptr::operator uint64_t()
{
    LOG("MyOptr::operator uint64_t()");
    return 0;
}
MyOptr::operator int64_t()
{
    LOG("MyOptr::operator int64_t()");
    return 0;
}
MyOptr& MyOptr::operator=(uint16_t val)
{
    LOG("MyOptr& MyOptr::operator=(uint16_t val)");
}
MyOptr& MyOptr::operator=(int16_t val)
{
    LOG("MyOptr& MyOptr::operator=(int16_t val)");
}
MyOptr& MyOptr::operator=(int32_t val)
{
    LOG("MyOptr& MyOptr::operator=(int32_t val)");
}
MyOptr& MyOptr::operator=(uint32_t val)
{
    LOG("MyOptr& MyOptr::operator=(uint32_t val)");
}
MyOptr& MyOptr::operator=(uint64_t val)
{
    LOG("MyOptr::operator=(uint64_t val)");
}
MyOptr& MyOptr::operator=(int64_t val)
{
    LOG("MyOptr::operator=(int64_t val)");
}
MyOptr& MyOptr::operator=(double val)
{
    LOG("MyOptr::operator=(double val)");
}
MyOptr& MyOptr::operator=(float val)
{
    LOG("MyOptr::operator=(float val)");
}
MyOptr MyOptr::operator[](unsigned int index) const
{
    LOG("MyOptr::operator[](unsigned int index) const");
    return *this;
}

MyOptr& MyOptr::operator<<(uint16_t val)
{
    LOG(" operator<<(uint16_t val);");
}
MyOptr& MyOptr::operator<<(int16_t val)
{
    LOG(" operator<<(int16_t val);");
}
MyOptr& MyOptr::operator<<(int32_t val)
{
    LOG(" operator<<(int32_t val);");
}
MyOptr& MyOptr::operator<<(uint32_t val)
{
    LOG(" operator<<(uint32_t val);");
}
MyOptr& MyOptr::operator<<(uint64_t val)
{
    LOG(" operator<<(uint64_t val);");
}
MyOptr& MyOptr::operator<<(int64_t val)
{
    LOG(" operator<<(int64_t val);");
}
MyOptr& MyOptr::operator<<(double val)
{
    LOG(" operator<<(double val);");
}
MyOptr& MyOptr::operator<<(float val)
{
    LOG(" operator<<(float val);");
}
root@jeason:~/cpp# g++ 1.cpp
root@jeason:~/cpp# ./a.out
this is MyOptr:118      MyOptr()
this is operator=:182   MyOptr& MyOptr::operator=(int32_t val)
this is operator=:198   MyOptr::operator=(double val)
this is operator=:202   MyOptr::operator=(float val)
this is operator=:194   MyOptr::operator=(int64_t val)
this is operator=:190   MyOptr::operator=(uint64_t val)
this is operator=:182   MyOptr& MyOptr::operator=(int32_t val)
this is operator=:178   MyOptr& MyOptr::operator=(int16_t val)
this is operator=:174   MyOptr& MyOptr::operator=(uint16_t val)
--------------------------1-------
this is operator<<:220   operator<<(int32_t val);
this is operator<<:236   operator<<(double val);
this is operator<<:240   operator<<(float val);
this is operator<<:232   operator<<(int64_t val);
this is operator<<:228   operator<<(uint64_t val);
this is operator<<:220   operator<<(int32_t val);
this is operator<<:216   operator<<(int16_t val);
this is operator<<:212   operator<<(uint16_t val);
--------------------------2-------
this is operator int32_t:144    MyOptr::operator int32_t()
this is operator double:159     MyOptr::operator double()
this is operator float:154      MyOptr::operator float()
this is operator int64_t:169    MyOptr::operator int64_t()
this is operator uint64_t:164   MyOptr::operator uint64_t()
this is operator int32_t:144    MyOptr::operator int32_t()
this is operator int16_t:139    MyOptr::operator int16_t()
this is operator uint16_t:134   MyOptr::operator uint16_t()
--------------------------3-------
this is operator[]:206  MyOptr::operator[](unsigned int index) const
this is MyOptr:130      MyOptr(const MyOptr& val)
this is operator=:174   MyOptr& MyOptr::operator=(uint16_t val)
--------------------------4-------
this is operator<<:113  template<typename T> MyOptr::operator<<
this is operator=:107   template<typename T> MyOptr::operator=

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-05-14 09:47:23  更:2022-05-14 09:48:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 19:33:53-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码