草庐IT

this_record

全部标签

c++ - enable_shared_from_this 需要什么?

这个问题在这里已经有了答案:Whatistheusefulnessof`enable_shared_from_this`?(6个答案)关闭6年前。我是C++11的新手,我遇到了enable_shared_from_this。我不明白它试图达到什么目的?所以我有一个使用enable_shared_from_this的程序。structTestCase:enable_shared_from_this{std::shared_ptrgetptr(){returnshared_from_this();}~TestCase(){std::coutobj1(newTestCase);std::sh

c++ - 将 "this"转换为引用指针

假设我有一个结构structFoo{voidbar(){do_baz(this);}/*Seeeditbelowvoiddo_baz(Foo*&pFoo){pFoo->p_sub_foo=newFoo;//forexample}*/Foo*p_sub_foo;}GCC告诉我temp.cpp:Inmemberfunction‘voidFoo::bar()’:temp.cpp:3:error:nomatchingfunctionforcallto‘Foo::do_baz(Foo*const)’temp.cpp:5:note:candidatesare:voidFoo::do_baz(Foo

c++ - 关于在 C++ 中将默认构造函数分配给 *this 的问题?

我正在阅读一些C++文本。在一个例子中,文本写成:classStudent{intno;chargrade[M+1];public:Student();Student(int,constchar*);constStudent&set(int,constchar*);voiddisplay()const;};Student::Student(){no=0;grade[0]='\0';}Student::Student(intn,constchar*g){*this=Student();//initializetoemptyset(n,g);//validate,resetifok}我不明

c++ - 编译器是否假定 "this"在 Debug模式下不是 nullptr?

我想知道在每个成员函数上放置assert(this!=nullptr);是否是个好主意。我相信编译器可以决定完全忽略这个断言,因为假设this不能为null,所以断言总是true并且可以在编译时解决-时间。但是如果编译器没有做出这个假设,那么这个断言对于及早发现问题非常有用。编译器会这样假设吗? 最佳答案 不,编译器通常不会这样假设。这些检查甚至还有商业代码,其中一些不仅断言而且实际上是逻辑。if(!this){doSomeWork();。虽然您无法在不遇到未定义行为的情况下达到this为NULL的情况,但如果您充分了解实现细节,那

C++ 错误 : ‘_mm_sin_ps’ was not declared in this scope

我正在尝试对将函数应用于数组的不同方法进行基准测试。为什么是https://software.intel.com/sites/landingpage/IntrinsicsGuide/#expand=3260,2124,4779,4779&cats=Trigonometry&text=_sin_mm_sin_ps在我的范围内未知,但_mm_sqrt_ps是?我如何让它为人所知?并编译无误。#include#include#include#include#include#include#include"immintrin.h"#includeintmain(){std::coutdis(-

c++ - 分配给右值 : why does this compile?

在下面的例子中:classA{private:doublecontent;public:A():content(0){}Aoperator+(constA&other){content+=other.content;return*this;}voidoperator=(constA&other){content=other.content;}};A是double的简单包装器,+和=运算符已被重载。在以下使用中:intmain(intargc,char*argv[]){Aa,b,c;(a+b)=c;//Whyisthisoperationlegal?}为什么(a+b)=c可以编译?我想知

c++ - std::this_thread::sleep_for(2s) 中的 s 是什么?

我找到了std::this_thread::sleep_for可以处理时间单位s。std::this_thread::sleep_for(2s);但是我不知道2s中的s是什么。 最佳答案 Whatissinstd::this_thread::sleep_for(2s)?s是一个用户定义的文字使得2schrono::second类型的文字值.内置文字您可能熟悉integerliterals和floatingliterals;这些是内置后缀:+--------+---------+---------------+|Suffix|Exam

c++ - 为什么 'this' 不是 volatile 的?

在过去的几天里调试了一个多线程,其中一个线程正在删除一个仍在被另一个线程使用的对象,我意识到如果我可以让“this”变得易变,那么诊断这个问题会容易得多,也快得多。它会将系统(SymbianOS)上的故障转储更改为包含更多信息的内容。那么,有什么理由不能或不应该这样吗?编辑:所以确实没有安全的方法来防止或检查这种情况。如果说访问陈旧类指针的一种解决方案是拥有一个保存指针的全局变量,并且任何被调用的函数都应该是使用全局变量替代“this”的静态函数,这是否正确?staticTAny*gGlobalPointer=NULL;#defineHarnessstatic_cast(gGlobal

c++ - 在 C++ 类中,使用 "this"访问成员变量有什么区别

我做了一个简单的类来代表一扇门。为了返回变量,我使用this指针访问它们。关于仅访问变量,使用this指针和不使用指针访问它们有什么区别?classDoor{protected:boolshut;//trueifshut,falseifnotshutpublic:Door();//Constructsashutdoor.boolisOpen();//Isthedooropen?voidOpen();//Opensthedoor,ifpossible.Bydefaultit//isalwayspossibletoopenagenericdoor.voidClose();//Shutsth

c# - 这里的 "this"关键字是什么意思?

一个非常短的C#函数。publicstaticintSizeInBytes(thisbyte[]a){returnsizeof(int)+a.Length*sizeof(byte);}这个函数中的“this”关键字是什么意思?C++中this关键字的等价物是什么?此外,这个函数试图准确计算什么? 最佳答案 它将方法标记为extensionmethod.扩展方法允许您扩展任何类的功能,即使它是密封的。例子:publicstaticclassStringExtensions{publicstaticboolIsEmpty(thisstr