草庐IT

this-page

全部标签

c++ - 在同一个对象上创建多个 shared_ptr "families"时 shared_from_this 的意外行为

下面是一些示例代码(在线here):#includestructFoo:publicstd::enable_shared_from_this{};voidexample(){autosharedFoo=std::make_shared();std::shared_ptrnonDeletingSharedFoo(sharedFoo.get(),[](void*){});nonDeletingSharedFoo.reset();sharedFoo->shared_from_this();//throwsstd::bad_weak_ptr}我看到的(在多个编译器下)是当nonDeleting

C++:即使使用 "this"关键字,参数声明也会隐藏类成员

我最近开始使用“警告级别4”,并在使用C++编码时将警告视为错误。我想要有关以下代码段的一些更深入的信息:structFoo{Foo(intsize)//:size{size}//size=size;}voidfunc(intsize){this->size=size;}intsize;};intmain(){Fooa{1};a.func(2);}因此,我从VisualStudio2019收到以下警告/错误:Error:C2220-Warningtreatedaserror-no'object'filegenerated.Warning:C4458-Declarationof'size

c++ - 非常量大小的数组 : Why does this even work?

这个问题在这里已经有了答案:C++:Whydoesintarray[size]work?(3个答案)关闭8年前。#includeusingnamespacestd;intmain(){intn;cout>n;intarray[n];//I'veworkedsomeoutputsanditworksreturn0;}这是某种动态分配吗?为什么它甚至不给出“n”为“const”的错误?另外,写cout不会导致编译时或运行时错误。我正在使用Dev-C++。

c++ - 访问修饰符在继承中的不同行为取决于 "this"关键字和模板或缺少它们

当涉及使用和/或省略template的4种组合时,我想了解访问修饰符关于继承的4种不同行为s和this关键词。以下所有代码均在g++4.8中完成:这是一个GrandChild类,privately继承自Parent,这privately继承自GrandParent,它有一个publicenumn.非对象,客户端代码可以访问GrandParent::n,因为后者是publicenum.但是GrandParent::n无法从内部访问GrandChild:#includeusingnamespacestd;structGrandParent{enum{n=0};};structParent:

c++ - `this == &x` 是确定指针 (this) 和引用 (x) 是否指向同一对象的正确方法吗?

我有一个类,在这个类中,有一个函数将同一类的另一个实例作为参数。在这个函数中,我需要做一些事情,但前提是传递的参数与当前参数不是同一个实例。以下代码是执行此操作的正确方法吗?classFoo{voidFoo::func(Foo&other){if(this!=&other)//dostuff}} 最佳答案 是的,(ptr==&ref)是确定指针和引用是否指向同一对象的正确方法。(从技术上讲,如果operator&过载,它可能会失败,但这是一种不太可能发生的情况,作为该类(class)的作者,您显然已经意识到这一点。)

C++ 错误 : was not declared in this scope with private after public

试图修改来自thispage的代码.问题代码如下:#include#includetemplateclassconst_reverse_wrapper{public:const_reverse_wrapper(constT&cont):container_(cont){}decltype(container_.rbegin())begin()const{returncontainer_.rbegin();}decltype(container_.rend())end(){returncontainer_.rend();}private:constT&container_;};templ

C++:将 this 指针传递给另一个类

我是C++的新手,但我确实有一些Java经验。在编码时,我偶然发现了一个让我困惑的错误。这是我的代码(经过简化,但错误相同):A.h:#pragmaonce#include"B.h"classA{public:A();voidfoo();voidsayHello();Bb;};A.cpp:#include"A.h"#includeA::A(){}voidA::foo(){b.bar(this);}voidA::sayHello(){std::coutB.h:#pragmaonce#include"A.h"classB{public:B();voidbar(A*a);};B.cpp:#i

c++ - shared_from_this 和私有(private)继承

cppreference有这个如何使用std::enable_shared_from_this的例子(略微调整)classGood:std::enable_shared_from_this{public:std::shared_ptrgetptr(){returnshared_from_this();}};...autogood=std::make_shared();good->getptr();但是,这在VisualStudio2015(企业版,版本14.0.25123.00更新2)中不起作用,即std::bad_weak_ptr抛出异常。查看其他示例(包括来自cppreferenc

c++ - 如何 std::forward( *this )

因为我想重载一个成员函数的所有cv和引用限定,我自己写了下面的宏:#defineDEFINE_FUNCTION(sig,functionality)\sig&{functionality;}\sig&&{functionality;}\sigconst&{functionality;}\sigconst&&{functionality;}\sigvolatile&{functionality;}\sigvolatile&&{functionality;}\sigconstvolatile&{functionality;}\sigconstvolatile&&{functionality

c++ - 为什么 'this' 指针在单步执行代码时会更改其值?

我正在调试崩溃,我注意到通过调试器的一个步骤,this指针更改了它的值,在3个步骤之后它最终获得了值0x00000001并且应用程序崩溃了。现在0x00000001值显然是错误的,但我真的应该期望this值在我逐步调试时发生变化吗?下面是我正在调试的构造函数崩溃的地方。我在每一步的注释中都包含了this指针的值,正如您所看到的,它会跳来跳去。CADOCommand::CADOCommand(CADODatabase*pAdoDatabase,CStringstrCommandText,intnCommandType){m_pCommand=NULL;m_pCommand.CreateI