偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

C++代碼賞析:回調(diào)中對象?;?/h1>

開發(fā) 前端
您可能希望將this指針捕獲到c++ lambda中,但這將捕獲原始指針。如果需要延長對象的生命周期,則需要捕獲強(qiáng)引用?!安东@對自己的強(qiáng)引用”的常見模式是同時(shí)捕獲強(qiáng)引用和原始this。強(qiáng)引用保持this為活的,并且使用this方便訪問成員。

概念

  • 類模板 std::function 是通用多態(tài)函數(shù)包裝器。 std::function 的實(shí)例能存儲(chǔ)、復(fù)制及調(diào)用任何可復(fù)制構(gòu)造(CopyConstructible)可調(diào)用(Callable)目標(biāo)——函數(shù)、 lambda 表達(dá)式、 bind 表達(dá)式或其他函數(shù)對象,還有指向成員函數(shù)指針和指向數(shù)據(jù)成員指針。
  • std::enable_shared_from_this 能讓其一個(gè)對象(假設(shè)其名為 t ,且已被一個(gè) std::shared_ptr 對象 pt 管理)安全地生成其他額外的 std::shared_ptr 實(shí)例(假設(shè)名為 pt1, pt2, ... ) ,它們與 pt 共享對象 t 的所有權(quán)。

例子1

您可能希望將this指針捕獲到c++ lambda中,但這將捕獲原始指針。如果需要延長對象的生命周期,則需要捕獲強(qiáng)引用?!安东@對自己的強(qiáng)引用”的常見模式是同時(shí)捕獲強(qiáng)引用和原始this。強(qiáng)引用保持this為活的,并且使用this方便訪問成員。

#include <functional>
#include <iostream>
#include <memory>
#include <string>

std::vector<std::function<void(void)>> actions;

class Widget : public std::enable_shared_from_this<Widget> {
public:
Widget(const std::string name){name_ = name;}
void reg(){
// std::shared_ptr
auto callback = [lifetime = shared_from_this(), this]() {
action(name_);
};
actions.push_back(callback);
}

virtual void action(std::string name){
std::cout << "widget action:" << name << std::endl;
}
std::string name_;
};
class Table : public Widget {
public:
Table(const std::string name):Widget(name){}
virtual void action(std::string name){
std::cout << "table action:" << name << std::endl;
}
};

void reg_action(){
auto widget = std::make_shared<Widget>("widget");
widget->reg();
auto table = std::make_shared<Table>("table");
table->reg();
}

int main(int argc, char* argv[]){
reg_action();
for (const auto& action : actions) {
action();
}
}

輸出:

widget action:widget
table action:table

在線測試

https://wandbox.org/permlink/HDrKO6Hn6tROiVEj

例子2

#include <functional>
#include <iostream>
#include <memory>

std::vector<std::function<void(void)>> actions;

class Widget : public std::enable_shared_from_this<Widget> {
public:
void reg(){
actions.push_back(std::bind(&Widget::action, shared_from_this()));
}

virtual void action(){
std::cout << "widget action" << std::endl;
}
};

class Table : public Widget {
public:
virtual void action(){
std::cout << "table action" << std::endl;
}
};

void reg_action(){
auto widget = std::make_shared<Widget>();
widget->reg();
auto table = std::make_shared<Table>();
table->reg();
}

int main(int argc, char* argv[]){
reg_action();
for (const auto& action : actions) {
action();
}
}

輸出:

widget action
table action


責(zé)任編輯:武曉燕 來源: 今日頭條
相關(guān)推薦

2010-02-04 16:07:39

C++回調(diào)函數(shù)

2023-01-03 13:30:14

C++代碼map

2022-10-24 08:03:04

MySQL數(shù)據(jù)庫

2010-01-21 10:23:53

C++代碼

2011-06-15 11:05:14

C語言回調(diào)函數(shù)

2009-08-19 16:40:35

C#回調(diào)

2009-08-12 10:11:18

C# 回調(diào)函數(shù)

2010-01-20 09:48:44

面向?qū)ο?/a>

2021-06-07 09:44:10

JavaScript開發(fā)代碼

2010-03-17 08:49:49

Visual Stud

2009-08-19 17:10:09

C#回調(diào)函數(shù)

2025-03-24 07:20:00

2023-11-10 16:31:31

2010-01-20 18:24:51

C++CLI

2023-10-30 10:29:50

C++最小二乘法

2009-07-31 16:25:29

C#回調(diào)函數(shù)API應(yīng)用

2010-01-21 09:34:57

C++語法

2010-01-27 16:05:06

C++堆棧

2011-06-21 10:17:41

c++內(nèi)存模型

2010-02-03 14:18:44

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)