栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

嵌入式linux学习笔记-- c++ 的 操作符重载

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

嵌入式linux学习笔记-- c++ 的 操作符重载

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

#include 
#define LOG(str) {printf("this is %s:%d t%sn",__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
    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
    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
    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
MyOptr::operator T()
{
    LOG("template MyOptr::operator T");
    return T{0};
}
template
MyOptr& MyOptr::operator=(T val)
{
    LOG("template MyOptr::operator=");
}

template
MyOptr& MyOptr::operator<<(T val)
{
    LOG("template 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 MyOptr::operator<<
this is operator=:107   template MyOptr::operator=

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/867283.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号