草庐IT

understanding-weak-references

全部标签

c++ - 是否有与 shared_from_this 等效的 weak_ptr?

我有一个类,我知道它将永远属于std::shared_ptr。然而,将shared_ptr甚至weak_ptr传递给不需要所有权或生命周期保证的函数和方法会产生不必要的开销。为了解决这个问题,我经常将原始指针传递给函数。该类本身继承自std::enable_shared_from_this,因此如果函数需要获取指针的所有权,它可以使用该类的方法来获取shared_ptr。一切都很顺利。然而,有些情况下我真的不想从原始指针创建一个shared_ptr,我想要的是一个weak_ptr。根据我对std::shared_ptr的通常实现的理解,它有两个用作引用计数器的原子变量;一个用于shar

c++ - 没有 main() 的 Cpp 类中的 `undefined reference to ` main`

我遇到了this在试图得到答案时。但似乎张贴者有多个文件,但没有链接,因此出现错误。但是,为什么在使用单个文件时会出现此错误?g++myClass.cpp/usr/lib/gcc/i686-redhat-linux/4.6.3/../../../crt1.o:Infunction`_start':(.text+0x18):undefinedreferenceto`main'collect2:ldreturned1exitstatus为什么main在编译时是必需的(它从哪里找到我的代码中提到的main)?main是代码执行的起点,但为什么编译器会假设我在这里需要一个main。我可以在其他

c++ - undefined reference `pthread_create' 使用 ASIO 和 std::thread 制作 C++11 应用程序时出错

我设置了Eclipse(实际上是XilinxSDK,但基于Eclipse)和g++4.9.2来编译一个使用独立ASIO的项目,我在属性->C/C++中使用了-std=c++11Build->Settings->ToolSettings->Otherflags以便它可以使用所有C++11特性进行编译。我还在C/C++GeneralSymbols中设置了ASIO_HAS_STD_THREAD,ASIO_STANDALONE等等,我希望ASIO头文件会使用std::thread而不是线程。但是,我仍然看到来自make的错误:undefinedreferencetopthread_create

c++ - 我可以从 boost 的 weak_ptr 获得原始指针吗?

是否可以从boost::weak_ptr获取原始指针?Boost的shared_ptr有get()方法和“->”运算符。weak_ptr不具有相同功能背后有什么理由吗? 最佳答案 weak_ptr拥有一个非拥有引用,因此它所引用的对象可能不再存在。使用weak_ptr持有的原始指针本质上是危险的。正确的方法是使用weak_ptr::lock()将weak_ptrboost为shared_ptr并从中获取指针。Boostweak_ptrdocumentation解释了为什么将get()功能作为weak_ptr的一部分提供是不安全的,并

c++ - boost 测试 - 'undefined reference' 错误

我有两个简单的文件:运行者.cpp:#defineBOOST_TEST_DYN_LINK#defineBOOST_TEST_MODULEMain#include和test1.cpp:#defineBOOST_TEST_DYN_LINK#ifdefSTAND_ALONE#defineBOOST_TEST_MODULEMain#endif#includeBOOST_AUTO_TEST_SUITE(Foo)BOOST_AUTO_TEST_CASE(TestSomething){BOOST_CHECK(true);}BOOST_AUTO_TEST_SUITE_END()为了编译,我正在使用:$

c++ - 链接器错误 : undefined reference to symbol 'pthread_rwlock_trywrlock@@GLIBC_2.2.5'

我一直在使用CentOS、Qt4.7和GCC4.4进行开发我刚刚安装了包含GCC4.7.2的RedHatDeveloperToolset1.1,在make结束时,我收到一个错误/usr/bin/ld:../../bin/Solo:undefinedreferencetosymbol'pthread_rwlock_trywrlock@@GLIBC_2.2.5'/usr/bin/ld:note:'pthread_rwlock_trywrlock@@GLIBC_2.2.5'isdefinedinDSO/lib64/libpthread.so.0sotryaddingittothelinker

c++ - 对 'Class::Class' 的 undefined reference

解决上一个问题后(请参阅我提出的另一个问题)。我已经宣布了更多类(class)。其中一个叫做CombatAdmin,它做各种事情:(头文件)#ifndefCOMBATADMIN_H#defineCOMBATADMIN_H#include//Needthislineoritcomplains#include#include#include#includeusingnamespacestd;classEnemy;classPlayer;classCombatAdmin//Codeyettobecommentedhere,willcomesoon.{public:CombatAdmin();

C++ 对 WinMain@16 的 undefined reference (Code::Blocks)

我正在使用Code::Blocks学习C++,每次我尝试创建一个新类时,我都会收到一条错误消息:undefinedreferenceto`WinMain@16'这是我一直在使用的代码:主类#include"Lime.h"#includeusingnamespacestd;intmain(){Limelime;return0;}青柠类(.ccp):#include"Lime.h"#includeusingnamespacestd;Lime::Lime(){cout石灰header(.h):#ifndefLIME_H#defineLIME_HclassLime{public:Lime();

objective-c - Xcode 7.3 无法使用手动引用计数在文件中创建 __weak 引用

更新到Xcode7.3后,它会在pod文件中抛出错误Cannotcreate__weakreferenceinfileusingmanualreferencecounting。有人解决过这个问题吗? 最佳答案 将BuildSettings->AppleLLVM7.1-Language-ObjectiveC->WeakReferencesinManualRetainRelease设置为YES。取自AppleDevelopersForums-Xcode7.3b4,non-arc,cannotcreate__weakreference.

Python 爬虫之 requests模块(ua伪装、代理、cookies、防盗链 Referer)、re模块、xpath模块、selenium

Python爬虫requests下载pipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simplerequests发送get请求案例:百度importrequestsurl="http://www.baidu.com"#发送get请求response=requests.get(url)#设置字符集(根据爬取网页charset=utf-8)response.encoding='utf8'#获取网页源代码print(response.text)发送post请求案例:百度翻译importrequestsurl="https://fanyi.baidu.com