析构函数未调用默认和已删除的构造函数

时间:2018-04-11 06:18:21

标签: c++ c++11 c++14

我一直在玩,并对以下代码感到好奇。

  @override
  Widget build(BuildContext context) {
    return new FutureBuilder(
      future: getTextFromFile(),
      initialData: "Loading text..",
      builder: (BuildContext context, AsyncSnapshot<String> text) {
        return new SingleChildScrollView(
          padding: new EdgeInsets.all(8.0),
          child: new Text(
            text.data,
            style: new TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 19.0,
            ),
          ));
      });
  }

  Future<String> getFileData(String path) async {
    return await new Future(() => "test text");
  }

  Future<String> getTextFromFile() async {
    return getFileData("test.txt");
  }
}

问题

  1. 为什么不同时为(A)和(B)调用析构函数?
  2. struct Bar { Bar()=delete; ~Bar() { std::cout << "dtor" << std::endl; } }; struct Foo { Foo(){} ~Foo() { std::cout << "dtor" << std::endl; } }; Bar b(); // (A) doesn't call dtor Foo f(); // (B) doesn't call dtor Foo f; // (C) calls dtor Bar b; // (D) error: use of deleted function 'Bar::Bar()' 还有一些魔力吗?

0 个答案:

没有答案