#include// std::cout using namespace std; class CMyClassaa { public: CMyClassaa(int x, int y); int m_x; int m_y=5; }; CMyClassaa::CMyClassaa(int x, int y) : m_y(y), m_x(m_y) { } int main() { CMyClassaa abc(1,2); cout << abc.m_x << ',' << abc.m_y << endl; return 0; }
结果:
32764,2
abc.m_x成员的初始化为不确定值。
锁的使用c++#include// std::cout #include // std::thread //#include #include #include using namespace std; template struct Node { Node(const T &value) : data(value) { } T data; Node *next = nullptr; }; template class WithLockListaa { mutex mtx; Node *head; public: void pushFront(const T &value) { auto *node = new Node (value); lock_guard csd(mtx); //表示一个类 node->next = head; head = node; } }; template class LockFreeListbb { struct atomic *> head; public: void pushFront(const T &value) { auto *node = new Node (value); node->next = head.load(); while(!head.compare_exchange_weak(node->next, node)); } }; struct adopt_lock_t_abcs { int a; adopt_lock_t_abcs()=default; }; adopt_lock_t_abcs aaa { }; int main() { const int SIZE = 1000000; auto start = chrono::steady_clock::now(); WithLockListaa wlList; for(int i = 0; i < SIZE; ++i) { wlList.pushFront(i); } auto end = chrono::steady_clock::now(); chrono::duration micro = end - start; cout << "--------- lock list costs micro:" << micro.count() << endl; start = chrono::steady_clock::now(); LockFreeListbb lfList; for(int i = 0; i < SIZE; ++i) { lfList.pushFront(i); } end = chrono::steady_clock::now(); micro = end - start; cout << "********* lock list costs micro:"<< micro.count() << endl; return 0; }
结果:
--------- lock list costs micro:42004.7 ********* lock list costs micro:56856.4
std_mutex.h: template命名空间c++class lock_guard { public: typedef _Mutex mutex_type; explicit lock_guard(mutex_type& __m) : _M_device(__m) { _M_device.lock(); } lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m) { } // calling thread owns mutex ~lock_guard() { _M_device.unlock(); } lock_guard(const lock_guard&) = delete; lock_guard& operator=(const lock_guard&) = delete; private: mutex_type& _M_device; };
#includeusing namespace std; namespace test2{ void func(){ cout << "11111" << endl; } int b=5; } struct node { int data; void init(int a) { this->data = a; } //类内定义的构造函数没有带分号';' node() :data(5) {} //无参数的构造函 node(int a) :data(a) {}//有参数的构造函数 }aaNode[10]; namespace test { struct test_node { int data; }tes_Node[10]; int b=10; void func(){ cout << "22222" << endl; } } using namespace test; using namespace std; //using namespace test2; //多次声明含有同样函数名或变量名的命名空间会出错 int main() { aaNode[0] = {2}; node aaa; cout<<"aaa.data="< 结果:
aaa.data=5 b=10 22222



