首页 > 其他分享 >Pointer-like classes像指针又像函数

Pointer-like classes像指针又像函数

时间:2024-03-31 13:33:17浏览次数:17  
标签:__ function const like type classes operator Pointer 指针

Pointer-like classes像指针又像函数

智能指针概念:

一个类做出来像类又像指针

示例代码:

#pragma once
#ifndef __SHAREPOINTER__
#define __SHAREPOINTER__

template<class T>
class shared_ptr
{
public:
shared_ptr(T* p) : px(p) { }
T& operator*() const { return *px; }
T* operator->() const { return px; }
private:
T* px;
long* pn;
};

#endif // !__SHAREPOINTER__

智能指针做两件事情:

  • 能够返回智能指针包裹对象的解引用(地址)

  • 能够通过智能指针调用包裹对象的方法

示例代码:

struct Foo
{
   void method(void) {return 0;}
}

shared_ptr<Foo> sp(new Foo); // 这样就创建了一根Foo类的指针

Foo f(*sp); // 调用的是shared_ptr头文件定义的operator*操作符重载方法,返回智能指针包裹对象的地址

sp->method(); // 调用Foo类里面的method()方法 -> 通过指针调用

迭代器

链表的设计:

节点示例代码:

#pragma once
#ifndef __LISTNODE__
#define __LISTNODE__

template <class T>
struct __list_node
{
void* prev;
void* next;
T data;
};

#endif // !__LISTNODE_

链表迭代器使用:

#pragma once
#ifndef __LISTITERATOR__
#define __LISTITERATOR__

template<class T, class Ref, class Ptr>
struct __list_iterator
{
typedef __list_node<T>* link_type;
link_type node;

T& operator*() const { return (*node).data; }
T* operator->() const { return&(operator*()); }
};

#endif // !__LISTOTERATOR__

function-like classes 仿函数

()操作符重载,对象可以接受()所以称之为function-like classes

示例代码:

#pragma once
#ifndef __PAIR__
#define __PAIR__

template<class T1, class T2>
struct pair
{
T1 first;
T2 second;
pair() : first(T1()), second(T2()) {}
pair(const T1& a, const T2& b) : first(a), second(b) {}
};

template<class Arg, class Result>
struct unary_function
{
typedef Arg argument_type;
typedef Result result_type;
};

template<class Arg1, class Arg2, class Result>
struct binary_function
{
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};

#endif // !__PAIR__

使用示例:

#pragma once
#ifndef __PAIRREALLY__
#define __PAIRREALLY__

template<class T>
struct identity : public unary_function<T, T>
{
const T&;
operator() (const T& x) const { return x; }
};

template<class Pair>
struct select1st : public unary_function<Pair, typename Pair::first_type>
{
const typename Pair::first_type&;
operator() (const Pair& x) const { return x.first; }
};

template<class Pair>
struct select2nd : public unary_function<Pair, typename Pair::second_type>
{
const typename Pair::second_type&;
operator() (const Pair& x) const { return x.second; }
};

#endif // !__PAIRREALLY__

unary_functionbinary_function都是仿函数

 

标签:__,function,const,like,type,classes,operator,Pointer,指针
From: https://www.cnblogs.com/JunkingBoy/p/18106643

相关文章

  • MySQL查询语句like_between_and_in
    数据表创建DDLCREATETABLE`student`(`id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'学号',`creatDate`datetimeDEFAULTNULLCOMMENT'创建时间',`userName`varchar(20)DEFAULTNULLCOMMENT'用户名',`pwd`varchar(36)DEFAULTNULL......
  • MySQL查询语句like_between_and_in
    1、创建数据表——DDL语句CREATETABLE`student`(`id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'学号',`createDate`datetimeDEFAULTNULLCOMMENT'创建时间',`userName`varchar(20)DEFAULTNULLCOMMENT'用户名',`pwd`varchar(36)DEFAUL......
  • Class with pointer member(s) -> 带指针的类
    Classwithpointermember(s)->带指针的类String字符串为讲解对象设计一个Class先设计头文件示例代码:#pragmaonce​//防卫式声明#ifndef__STRING__#define__STRING__​#include<string.h>​//头文件定义classString{public://设计构造函数->这里只是定义了......
  • MySQL学习必备查询语句like_between and_in
     MySQL创建数据库需要自行创建数据库名称可以为【schoolDB】,字符集【utf8】,排序规则【utf8_general_ci】,建表操作:CREATETABLE`student`( `id`INT(11)NOTNULLAUTO_INCREMENTCOMMENT'学号', `createDate`datetimeDEFAULTNULL, `userName`VARCHAR(20......
  • MySQL各类查询语句DQL——like_between and_null_in
    创建数据库CREATETABLE`student`(`id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'学号',`createDate`datetimeDEFAULTNULL,`userName`varchar(20)DEFAULTNULL,`pwd`varchar(36)DEFAULTNULL,`phone`varchar(11)DEFAULTNULL,`age`tinyin......
  • MySQL各类查询语句DQL--like_in_between_and
    建表语句CREATETABLE`student`(`id`int(11)NOTNULLAUTO_INCREMENT,`createDate`datetimeDEFAULTNULL,`userName`varchar(20)DEFAULTNULL,`pwd`varchar(36)DEFAULTNULL,`phone`varchar(11)DEFAULTNULL,`age`tinyint(3)DEFAULTNULL,......
  • MySQL各类查询语句DQL--like in between...and
    数据库名称可以为【schoolDB】,字符集【utf8】,排列规则【utf8_general_ci】建立表CREATETABLE`student`(`id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'学号',`createDate`datetimeDEFAULTNULL,`userName`varchar(20)DEFAULTNULL,`pwd`varchar(36)DE......
  • QT 智能指针 QPointer QScopedPointer QSharedPointer QWeakPointer QSharedDataPoint
    QPointerQPointer是一种受保护的指针,当其引用的对象被销毁时,它会被自动清除(但是,销毁引用对象还是必须手动delete)。QPointer所指向的对象必须是QObject或其派生类对象。当多个指针指向同一个Object对象时,引用的对象可能被释放掉,这时使用QPointer就可以安全的测试引用对象是......
  • MyBatis plus之foreach、like、likeRigth、likeLeft
    文章目录1.foreach2.like3.likeLeft4.likeRight1.foreachMybatisPlus提供了foreach标签来简化在查询语句中使用in语句的操作。通过foreach可以将一个集合中的元素拼接成一个逗号分隔的字符串,并将其作为in语句的参数。例如,我们可以使用foreach来生成一个动态的......
  • Minimal BASH-like line editingis supported解决方法
    一、问题原因        Windows/Linux双启动的电脑一般都使用grub作为引导程序,出现这个原因,是windows启动的时候,没有找到启动文件。我是因为安装了windows和Linux双系统,卸载Ubuntu系统时,没有完全删除Ubuntu的相关信息,重启电脑将无法正常进入Windows系统,开机后一直卡在gr......