从C++ FAQ:
[11.4]我可以为我的班级重载析构函数吗?没有.
我意识到这意味着你不能改变返回类型,参数的类型和参数的数量.我可能会分析单词的语法,但是可以覆盖 Parent的析构函数吗?
class Child : public Parent {
public:
virtual Parent::~Parent() {
// New definition
}
};
Run Code Online (Sandbox Code Playgroud)
那个问题是递归吗?
class Grandchild : public Child {
public:
Child::Parent::~Parent() {
// An even newer definition
}
};
Run Code Online (Sandbox Code Playgroud)
我已经阅读了这个和相关的帖子,它让我思考,因为析构函数不是继承的,它们不能被覆盖,但我从未见过它明确陈述过.
编辑:我改变了这一点,以反映我想覆盖Parent的析构函数,注意Child和Grandchild重写~Parent().
我这样做的主要原因是在改变它的销毁方式时保持Parent的界面(子类的完整原因).我将拥有管理所有Parent的其他内容,并将在我选择的稍后时间显式调用它们的析构函数.
也许我错过了std::asyncC++ 11中新的正确用法,但是这句话(在cppreference.com上):
如果设置了异步标志(即policy&std :: launch :: async!= 0),则async在单独的执行线程上执行函数f,就好像由std :: thread(f,args ...)生成的一样.除了如果函数f返回一个值或抛出异常,它将存储在可通过std :: future访问的共享状态中,async返回给调用者.
让我觉得我的线程应该立即开始这个声明:
std::async(std::launch::async, MyFunctionObject());
Run Code Online (Sandbox Code Playgroud)
无需等待呼叫std::future::get().这似乎不是这种情况(使用MSVC 13进行编译).如果这不是由这个语句本身触发的,如果我不关心std::future对象的返回值,应该如何触发?
例:
#include <thread>
#include <iostream>
#include <array>
#include <future>
static std::mutex write_mutex;
class Cpp11Threads {
public:
// Function operator for Function-Object
void operator()() {
const int num_threads = 50;
// Static std array
std::array<std::thread*, num_threads> worker_threads;
// Range based
for (std::thread*& thread : worker_threads) {
// Lambda expression
thread = new std::thread(
[] { …Run Code Online (Sandbox Code Playgroud) 当std::make_shared(new Foo())调用它时,它构造一个Foo并std::shared_ptr<Foo>为调用者返回一个(即这里).如果从各种对象多次调用它,它new Foo()每次构造一次吗?在这种情况下,它是否与每个调用者获得对新对象的单个引用没有什么不同,在实用性上像unqiue_ptr一样?
或者它Foo()是第一次创建一个然后随后返回std::shared_ptr它,知道它将像一个单独的类型(当然删除一旦最后一个std::shared_ptr被删除)?这个平台是否具体?
特别是这样的功能:
std::shared_ptr<Foo> makeFoo()
{
return std::make_shared<Foo>();
}
Run Code Online (Sandbox Code Playgroud)