intmain(){ Test t; cout << "==========" << endl; A a(t); cout << "==========" << endl; B b(t); }
输出:
1 2 3 4 5 6 7 8 9
@└────> # ./a.out constructor called ========== copy constructor called // A 中 ele 的拷贝构造 constructor called // A 中 t_ 的构造函数 assignment operator called // A 中 = ele 的赋值调用 ========== copy constructor called // B 中 ele 的拷贝构造 copy constructor called // B 中 t_ 的初始化(拷贝构造)
可以看到上面对于 A 来说,是先调用构造函数再调用赋值函数,而对于 B 来说就是拷贝构造函数。所以列表的形式是初始化,不同于在大括号内赋值。
inttest(int a, int b){ std::this_thread::sleep_for(std::chrono::seconds(1)); std::lock_guard<std::mutex> lock(g_screen_mutex); std::cout << "test() at thread [ " << std::this_thread::get_id() << "] output [" << a + b << "]" << std::endl; return a + b; }
intmain(){ thread_pool thread_pool; std::vector<std::future<int>> results; for(int i = 0; i < 5 ; i++) { auto fu = thread_pool.add_task(test, i, 2); results.emplace_back(std::move(fu)); } getchar(); // 等待,不要让主进程退出 for (int i = 0; i < 5; i++) { std::cout<< "result of number " << i << " :" << results[i].get() << std::endl; } return0; }
@└────> # ./test.out thread_pool::threadLoop() tid : 139812637472512 start. thread_pool::take() tid : 139812637472512 wakeup. thread_pool::take() tid : 139812637472512 took a task! thread_pool::threadLoop() tid : 139812629079808 start. thread_pool::take() tid : 139812629079808 wakeup. thread_pool::take() tid : 139812629079808 took a task! thread_pool::threadLoop() tid : 139812620687104 start. thread_pool::take() tid : 139812620687104 wakeup. thread_pool::take() tid : 139812620687104 took a task! test() at thread [ 139812637472512] output [2] thread_pool::take() tid : 139812637472512 wakeup. thread_pool::take() tid : 139812637472512 took a task! test() at thread [ 139812629079808] output [3] thread_pool::take() tid : 139812629079808 wakeup. thread_pool::take() tid : 139812629079808 took a task! test() at thread [ 139812620687104] output [4] thread_pool::take() tid : 139812620687104 wait. test() at thread [ 139812637472512] output [5] thread_pool::take() tid : 139812637472512 wait. test() at thread [ 139812629079808] output [6] thread_pool::take() tid : 139812629079808 wait. (键入回车) result of number 0 :2 result of number 1 :3 result of number 2 :4 result of number 3 :5 result of number 4 :6 thread_pool::stop() stop. thread_pool::stop() notifyAll(). thread_pool::take() tid : 139812620687104 wakeup. thread_pool::threadLoop() tid : 139812620687104 exit. thread_pool::take() tid : 139812637472512 wakeup. thread_pool::threadLoop() tid : 139812637472512 exit. thread_pool::take() tid : 139812629079808 wakeup. thread_pool::threadLoop() tid : 139812629079808 exit.
@└────> # ./a.out thread_pool::threadLoop() tid : 139841310938880 start. thread_pool::take() tid : 139841310938880 wakeup. thread_pool::take() tid : 139841310938880 took a task! thread_pool::threadLoop() tid : 139841302546176 start. thread_pool::take() tid : 139841302546176 wakeup. thread_pool::take() tid : 139841302546176 took a task! thread_pool::threadLoop() tid : 139841294153472 start. thread_pool::take() tid : 139841294153472 wakeup. thread_pool::take() tid : 139841294153472 took a task! =============================================================== | testFunc() at thread [ 139841310938880] output [0] | =============================================================== thread_pool::take() tid : 139841310938880 wakeup. thread_pool::take() tid : 139841310938880 took a task! =============================================================== | testFunc() at thread [ 139841302546176] output [0] | =============================================================== thread_pool::take() tid : 139841302546176 wakeup. thread_pool::take() tid : 139841302546176 took a task! =============================================================== | testFunc() at thread [ 139841294153472] output [0] | =============================================================== thread_pool::take() tid : 139841294153472 wait. =============================================================== | testFunc() at thread [ 139841310938880] output [0] | =============================================================== thread_pool::take() tid : 139841310938880 wait. =============================================================== | testFunc() at thread [ 139841302546176] output [0] | =============================================================== thread_pool::take() tid : 139841302546176 wait. (键入回车) thread_pool::stop() stop. thread_pool::stop() notifyAll(). thread_pool::take() tid : 139841294153472 wakeup. thread_pool::threadLoop() tid : 139841294153472 exit. thread_pool::take() tid : 139841310938880 wakeup. thread_pool::threadLoop() tid : 139841310938880 exit. thread_pool::take() tid : 139841302546176 wakeup. thread_pool::threadLoop() tid : 139841302546176 exit.
@└────> # g++ main.cc -c @└────> # g++ A.cc -c @└────> # nm main.o 0000000000000000 T main U _ZN1A5printEv @└────> # nm A.o U __cxa_atexit U __dso_handle 000000000000006c t _GLOBAL__sub_I__ZN1A5printEv 000000000000002e t _Z41__static_initialization_and_destruction_0ii 0000000000000000 T _ZN1A5printEv U _ZNSolsEi U _ZNSolsEPFRSoS_E U _ZNSt8ios_base4InitC1Ev U _ZNSt8ios_base4InitD1Ev U _ZSt4cout U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_ 0000000000000000 r _ZStL19piecewise_construct 0000000000000000 b _ZStL8__ioinit
@└────> # g++ main.cc -c @└────> # g++ A.cc -c @└────> # nm main.o 0000000000000000 T main U _ZN1A5printIiEEvv @└────> # nm A.o U __cxa_atexit U __dso_handle 000000000000003e t _GLOBAL__sub_I_A.cc 0000000000000000 t _Z41__static_initialization_and_destruction_0ii U _ZNSt8ios_base4InitC1Ev U _ZNSt8ios_base4InitD1Ev 0000000000000000 r _ZStL19piecewise_construct 0000000000000000 b _ZStL8__ioinit
#include<iostream> classA { public: int m; template<class T> voidprint(){ std::cout << m << std::endl; } };
编译:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
@└────> # g++ main.cc -c @└────> # nm main.o U __cxa_atexit U __dso_handle 0000000000000059 t _GLOBAL__sub_I_main 0000000000000000 T main 000000000000001b t _Z41__static_initialization_and_destruction_0ii 0000000000000000 W _ZN1A5printIiEEvv U _ZNSolsEi U _ZNSolsEPFRSoS_E U _ZNSt8ios_base4InitC1Ev U _ZNSt8ios_base4InitD1Ev U _ZSt4cout U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_ 0000000000000000 r _ZStL19piecewise_construct 0000000000000000 b _ZStL8__ioinit
virtualinttest(){ // 不正确,不能修饰全局函数 // do some thing }
编译报错:
1
error: ‘virtual’ outside class declaration
2. virtual 不能修饰构造函数。
从存储空间角度,虚函数的每个对象内部都有一个指向虚函数表的指针。那么虚函数表是什么时候初始化的呢?就是在调用构造函数的时候。cpp 调用 new 的步骤是,先申请所需的内存(malloc/operator new),再调用构造函数。如果构造函数是虚的,那必须通过虚函数表指针来找到虚构造函数的入口地址。然而此时申请的内存还没有初始化,不可能由虚函数表和虚函数表指针的。综上所述,构造函数不能为虚函数。