![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++ Programming
文章平均质量分 66
jsc0218
I am a PhD student in University of Waterloo. This Blog is some notes about technical details I met.
展开
-
const member functions and const return values
Why there are two function's versions in one class(const and non-const)? Let's have a test now.#include using namespace std;class A{public: A() {} void fun() { cout<<"I am not const."<<en原创 2011-11-03 14:17:24 · 439 阅读 · 0 评论 -
C++四种强制类型转换运算符
C++有四种强制类型转换符,分别是dynamic_cast,const_cast,static_cast,reinterpret_cast。其中dynamic_cast与运行时类型转换密切相关,在这里我们先介绍dynamic_cast,其他三种在后面介绍。1、dynamic_cast运算符 该转换符用于将一个指向派生类的基类指针或引用转换为派生转载 2014-03-20 00:19:11 · 9952 阅读 · 2 评论 -
#undef的用处
如果你想定义这个宏那就#define X如果你不想让你已经#define X的宏在其他其他地方由于引入了这个包含宏的.h文件而引起一些编译问题,那你就#undef X掉,就这么简单。举个简单的例子。有1.c,11.h,12.h三个文件其中在11.h中定义了#define X externx int a;而在12.h中你又定义了#define X intx b;而在1转载 2014-03-25 23:04:55 · 765 阅读 · 0 评论 -
Plain Explanation of "memory_order"
memory_order_acquire: guarantees that subsequent loads are not moved before the current load or any preceding loads.memory_order_release: preceding stores are not moved past the current store or any s转载 2014-03-28 23:39:57 · 889 阅读 · 0 评论 -
C++11 range-based for loops
In the first article introducing C++11 I mentioned that C++11 will bring some nice usability improvements to the language. What I mean is that it removes unnecessary typing and other barriers to get转载 2014-03-26 12:08:31 · 1546 阅读 · 0 评论 -
C++11 Concurrency Tutorial – Part 2 : Protect shared data
In the previous article, we saw how to start threads to execute some code in parallel. All the code executed in the threads were independant. In the general case, you often use shared objects between转载 2014-03-26 13:19:56 · 765 阅读 · 0 评论 -
C++11 Concurrency Tutorial – Part 4: Atomic Types
In the previous article, we saw advanced techniques about mutexes. In this post, we will continue to work on mutexes with more advanced techniques. We will also study another concurrency technique of转载 2014-03-27 08:33:07 · 1044 阅读 · 0 评论 -
C++11 Synchronization Benchmark
In the previous parts of this series, we saw some C++11 Synchronization techniques: locks, lock guards and atomic references.In this small post, I will present the results of a little benchmark I转载 2014-03-27 09:15:44 · 832 阅读 · 0 评论 -
C++11 Concurrency – Part 1 : Start Threads
C++11 introduced a new thread library. This library includes utilities for starting and managing threads. It also contains utilities for synchronization like mutexes and other locks, atomic variables转载 2014-03-26 09:08:23 · 677 阅读 · 0 评论 -
C++11 Concurrency Tutorial – Part 3: Advanced locking and condition variables
In the previous article, we saw how to use mutexes to fix concurrency problems. In this post, we will continue to work on mutexes with more advanced techniques. We will also study another concurrency转载 2014-03-26 15:20:27 · 1097 阅读 · 0 评论 -
Static member functions in C++
In the previous lesson on static member variables, you learned that classes can have member variables that are shared across all objects of that class type. However, what if our static member variab转载 2014-09-28 01:35:31 · 695 阅读 · 0 评论 -
Static member variables in C++
Static keyword in CIn the lesson on file scope and the static keyword, you learned that static variables keep their values and are not destroyed even after they go out of scope. For example:转载 2014-09-28 01:20:30 · 1172 阅读 · 0 评论 -
C++ Singleton
Singleton in C++:#include using namespace std;class Restaurant {public: static Restaurant *getInstance() { if (instance == NULL) instance = new Restaurant(100); return instance; } int get原创 2014-09-28 01:42:42 · 1160 阅读 · 2 评论 -
Setting static library in Eclipse C++ in Linux
1. Build static library project from Eclipse.2. Create header (hello.h) and source (hello.cc) file.codes of hello.h#ifndef HELLO_H_#define HELLO_H_void output();#endif /* HEL原创 2014-02-15 12:00:04 · 1875 阅读 · 0 评论 -
Setting shared (dynamic) library in Eclipse C++ in Linux
1. Build shared library project from Eclipse.2. Create header (hello.h) and source (hello.cc) file. Build them to create the shared library.codes of hello.h#ifndef HELLO_H_#de原创 2014-02-15 13:12:16 · 1623 阅读 · 0 评论 -
gcc: when initialized here
There is a strange thing. Let us examine it now!#include using namespace std;class A {public: A() : b(0), a(0){}private: int a; int b;};int main () { return 0;}Warning! This is原创 2011-11-11 16:12:59 · 994 阅读 · 0 评论 -
How to code the forward declaration of class or struct using template?
This is a simple example.原创 2011-11-14 10:47:40 · 908 阅读 · 0 评论 -
Relationship of the this pointer and (non-)const member function
Let's see the example first.class A {public: A() { a = 0; } int& Get() { return a; } void Test() const { this->Get(); } void Test() { this->Get(); }private: int a;};int main() { A obj;原创 2011-11-16 16:23:25 · 395 阅读 · 0 评论 -
Convert skills in C++, including int, double, float, bool with string
#include // must#include using namespace std;int main(){ stringstream ss; /************ int->string ************/ int i = 12; string str; ss << i; ss >> str; cout << str << endl; /*****原创 2011-12-14 22:20:17 · 528 阅读 · 0 评论 -
Friend function should be a public function, if it is in a class.
See the code please.JSC.h#ifndef JSC_H_#define JSC_H_#include using namespace std;class JYJ;class JSC{public: JSC() {}//private: void look(const JYJ& jyj);};#include "JYJ.h"voi原创 2011-11-04 11:39:49 · 509 阅读 · 0 评论 -
How to use the pointer of function in a class?
Let us see the example first.#include using namespace std;class A {public: A (int func(int, int)) { compare = func; } // give value void Test (int a, int b) { cout<<compare(a, b)<<endl; }原创 2011-11-14 14:42:31 · 438 阅读 · 0 评论 -
Something about virtual function and pure virtual function
Firstly, let's demonstrate a fact that virtual function must be implemented.class A {public: A() { a = 0; } virtual void a1(); int a2();private: int a;};int main() { A a; retur原创 2011-11-15 16:05:03 · 373 阅读 · 0 评论 -
Some tricky problems related to "protected"
Firstly, let us see the wrong version.class A {protected: int price;};class C;class B: public A {public: void test(const A& a, const B& b, const C& c);};class C: public B {};void B::原创 2011-12-12 19:47:03 · 352 阅读 · 0 评论 -
Two ways using global objects in C++
1.Through includeGlobe.h#ifndef GLOBE_H_#define GLOBE_H_int g = 0;#endif /* GLOBE_H_ */A.h#ifndef A_H_#define A_H_#include "Globe.h" // through includeclass A {public: A() { g =原创 2011-12-20 21:55:29 · 416 阅读 · 0 评论 -
Understanding of extern "C"
In this example, there are two files, namely f1.c and test.cc.f1.cvoid f1(){ return;}test.ccextern "C" { extern void f1();}int main(){ f1(); return 0;}Compiler will compil原创 2012-01-01 18:08:39 · 335 阅读 · 0 评论 -
Difference between time_t and clock_t
The example:#include using namespace std;int main(){ time_t time_start, time_end; time(&time_start); sleep(5); time(&time_end); cout<<difftime(time_end, time_start)<<endl; // 5 clo原创 2012-03-21 10:11:17 · 473 阅读 · 0 评论 -
A simple implementation of string split in C++
Since string's split function is not naturally provided in C++, we must implement it by ourselves. Here is a simple implementation of split based on strtok.vector split(const string &str, const原创 2014-02-24 13:35:38 · 985 阅读 · 0 评论 -
C++ Singleton + MultiThread
#include #include using namespace std;template class Singleton {public: static T *instance() { if (object == NULL) { mtx.lock(); if (object == NULL) object = new T; mtx.unlock(); }原创 2014-10-17 23:41:14 · 912 阅读 · 0 评论