intmain(){ auto fu = std::async([](int a, int b) { std::this_thread::sleep_for(std::chrono::milliseconds(2000)); std::cout << "thread start!" << std::endl; return a + b; }, 10, 20);
intmain(){ std::async([](int a, int b) { std::this_thread::sleep_for(std::chrono::milliseconds(2000)); std::cout << "thread start!" << std::endl; return a + b; }, 10, 20);
分配内存。这一步底层就是调用了标题中提到的另一个 operator new 来完成的。也就是说最大的区别是 operator new 只负责分配内存,而 new 调用 operator new 并且调用构造函数。如果类 className 重载了 operator new(属于类里面的运算符),那么上述代码调用的就是
$ gdb a.exe GNU gdb(GDB) 7.6.1 Copyright(C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "mingw32". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from F:\zkangHUST\C++\a.exe...done. (gdb) start Temporary breakpoint 1 at 0x40146e: file test3.cpp, line 32. Starting program: F:\zkangHUST\C++/a.exe [New Thread 10860.0x2e0c] [New Thread 10860.0x3e64] [New Thread 10860.0x3e94] [New Thread 10860.0x8] Temporary breakpoint 1, main () at test3.cpp:32 32 A a; (gdb) n 33 B b; (gdb) 51return0; (gdb) p a $1 = {_vptr.A = 0x405178 <vtable for A+8>, a = 4194432} (gdb) p (int*)*((int*)0x405178) $2 = (int *) 0x403c08 <A::f()> (gdb) p (int*)*((int*)0x405178 + 1) $3 = (int *) 0x403c3c <A::g()> (gdb) p (int*)*((int*)0x405178 + 2) $4 = (int *) 0x0 (gdb) p b $5 = {<A> = {_vptr.A = 0x405188 <vtable for B+8>, a = 4200896}, b = 0} (gdb) p (int*)*((int*)0x405188) $6 = (int *) 0x403ca0 <B::f()> (gdb) p (int*)*((int*)0x405188+1) $7 = (int *) 0x403c3c <A::g()> (gdb) p (int*)*((int*)0x405188+2) $8 = (int *) 0x403cd4 <B::g1()> (gdb) p (int*)*((int*)0x405188+3) $9 = (int *) 0x3a434347 (gdb)
classD : public B, public C { public: D() { cout << "D()" << endl; } ~D() { cout << "~D()" << endl; } }; intmain(){ D d; // A* a = &d; // test.cc: In function ‘int main()’: // test.cc:46:13: error: ‘A’ is an ambiguous base of ‘D’ // A* a = &d; // ^ return0; }