this

Compilation error : 'this' cannot be implicitly captured in this context

本小妞迷上赌 提交于 2020-08-01 09:32:19
问题 I am trying to add a condition_variable to handle threads, but get a compilation error at this line: this->cv.wait(lk, []{return this->ready;}); Looks like the for the variable this->ready, the 'this' is not in the right scope. In Java this can be handled with TestThread.this, is there anything in C++ doing the same? void TestThread::Thread_Activity() { std::cout << "Thread started \n"; // Wait until ThreadA() sends data { std::unique_lock<std::mutex> lk(m); this->cv.wait(lk, []{return ready;

C++11 Passing 'this' as paramenter for std::make_shared

。_饼干妹妹 提交于 2020-07-18 04:17:22
问题 I'm trying to pass 'this' to the constructor using std::make_shared Example: // headers class A { public: std::shared_ptr<B> createB(); } class B { private: std::shared_ptr<A> a; public: B(std::shared_ptr<A>); } // source std::shared_ptr<B> A::createB() { auto b = std::make_shared<B>(this); // Compiler error (VS11 Beta) auto b = std::make_shared<B>(std::shared_ptr<A>(this)); // No compiler error, but doenst work return b; } However this does not work properly, any suggestions how I can