Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 C++


  About C++ comma operator precedence

When learning programming language, we will always have one topic about the operator precedence. Since there are many operators can be used in one expression, there should be some rules regarding which operation can happen first so that the compiler or interpreter can handle them correctly. One rule of programming with expression is using as much as brackets as you can to avoid unexpected results.Here we refer one problem we see online about the comma operator in C++. The following program has the result which some people may feel strange.The first code snippet:01020304050607080910void main ()...

6,026 1       COMMA PRECEDENCE


  GCC is compiled with C++ compiler

On 15 Aug, 2012, GCC merged a patch--Merge from cxx-conversion branch . This means GCC will be compiled with C++ compiler in the future, it also means that GCC will be implemented using C++.You may have following two puzzles:Why does GCC turn to C++?Without C++ compiler, how can we compile C++ codes?Why using C++?In GNU's C++ Conversion, we can find this description in the background section:Whether we use C or C++, we need to try to ensure that interfaces are easy to understand, that the code is reasonably modular, that the internal documentation corresponds to the code,&n...

363,559 0       C++ GCC COMPILER


  C++ : string beginWith and endWith

C++ is an very powerful programming language. It is efficient and flexible. When writing C++ programs, we may often need to process strings and often we need to check whether a string begin with some substring or end with some substring. We can use following functions to ahieve these:    static bool beginWith(const std::string str,const std::string needle){        return (!str.compare(0,needle.length(),needle));    }    static bool endWith(const std::string str,const std::string needle){       &...

7,705 1       C++ BEGINWITH ENDWITH


  Save QWidget as image

Qt library is an excellent GUI library for C++ programmers developed by Nokia and now is an open source project. Often, we may use QPainter to draw strings, lines or images on a QWidget. We can override the QWidget's paintEvent() method when we want to use QPianter object to draw something on a QWidget.If we want to save the items drawn on QWidget as image for later reference, what can we do? We can save a QWidget as an image. Here is the code for achieving this:QPixmap pixmap(this->size());this->render(&pixmap);pixmap.save("test.png");Quite simple, right? Yes, these are all the code...

20,821 1       C++ IMAGE QT QWIDGET


  Convert number to string in C++

Prior to C++11, there is no built-in function in C++ standard library which can be used to convert numbers such as interger and double number to string.  There are many ways which can convert number to string. Since C++ is C compatible, we can use itoa() function to convert an integer to C style string. But this one can only convert integer to string, not double. For different types of numbers, we need to use different functions.string s = string(itoa(a));Actually, we can also use stringstream in C++ to convert number to string.  Also, here since we want to convert different types of...

18,844 1       C++ CONVERSION NUMBER STRING


  new expression vs operator new in C++

In C++, there is a new mechanism for memory allocation and management. When we want to initialize an object, we can use new expression to get enough memory for storing an object and also initialize the object. When we want to create a new object, we can use new expression, for example:Obj *obj=new Obj;Basically, what the new expression does is to allocate enough memory for storing the obj object, in addition, it will initialize the object with some intial values.Also, there is an operator new existing in C++, what it does is to allocate raw memory. It only controls allocating memory, not inclu...

5,084 0       C++ NEW EXPRESSION OPERATOR NEW


  My favorite quotes from "The design and evolution of C++"

"The design and evolution of C++" is a book written by the inventor of C++  Bjarne stroustrup. In this book, Bjarne stroustrup presents the definitive insider's guide to the design and development of the C++ programming language. Without ommitting critical details or getting bogged down in technicalities, Stroustrup presents his unique insights into the decisions that shaped C++. In this book, some statements are very impressive. They are concise but convey much information. Here I summarize some statements which I like most in this book. A programming language can be the most important f...

4,992 0       C++ COMPATIBILITY TYPE SYSTEM STATEMENTS


  malloc/free and new/delete in C++

malloc and free are C++/C language standard library functions, while new/delete are operator of C++. They can be used to allocate dynamic memory and free memory in C++ programs malloc/free can not meet the requirements of dynamic objects creation. Object needs to call the constructor to initialize the object when creating, the object needs to call the destructor before it is destroyed  Since malloc() and free() are library functions rather than operators, the compiler has no control permissions on them, so it can not impose the task of object construction and destruction on malloc() and f...

14,539 1       MEMORY C++ DELETE FREE MALLOC NEW