This entry is continued from C++ Tips from Thinking in C++(3)
C++ Tips from Thinking in C++(3)
This entry is continued from C++ Tips from Thinking in C++(2)
C++ Tips from Thinking in C++(2)
Continued from C++ Tips from Thinking in C++(1)
5. Const pointer VS. Pointer to const
If you want to prevent any changes to the element you are pointing to, that is “Pointer to const”, you write a definition like this:
const int* u;
Starting from the identifier, we read “u is a pointer, which points to a const int.”
Here’s the mildly confusing part. You might think that to make the pointer itself unchangeable, that is, to prevent any change to the address contained inside u, you would simply move the const to the other side of the int like this:
int const* v;
Unfortunately, I will tell you that it’s wrong! Actually “const int* u;
” EQUALS “int const* v;
“. However, the way it actually reads is “v is an ordinary pointer to an int that happens to be const.” That is, the const has bound itself to the int again, and the effect is the same as the previous definition. So to avoid confusion, you should abandon the second form.
So how to make a “const pointer”? To make the pointer itself a const, you must place the const specifier to the right of the *, like this:
int d = 1;
int* const w = &d;
Now it reads: “w is a pointer, which is const, that points to an int.” Because the pointer itself is now the const, the compiler requires that it be given an initial value that will be unchanged for the life of that pointer.
You can also make a const pointer to a const object using either of two legal forms:
int d = 1;
const int* const x = &d; // (1)
int const* const x2 = &d; // (2)
Now neither the pointer nor the object can be changed.
6. Standard argument passing
In C it’s very common to pass by value, and when you want to pass an address your only choice is to use a pointer. However, in C++, Your first choice when passing an argument is to pass by reference, and by const reference at that. But, passing by reference produces a new situation that never occurs in C: a temporary, which is always const, can have its address passed to a function.So to allow temporaries to be passed to functions by reference, the argument must be a const reference.The following example demonstrates this: (c++11 introduced
class X {};
X f() { return X(); } // Return by value
void g1(X&) {} // Pass by non-const reference
void g2(const X&) {} // Pass by const reference
int main() {
// Error: const temporary created by f():
//! g1(f());
// OK: g2 takes a const reference:
g2(f());
} ///:~
f( ) returns an object of class X by value. That means when you immediately take the return value of f( ) and pass it to another function as in the calls to g1( ) and g2( ), a temporary is created and that temporary is const. Thus, the call in g1( ) is an error because g1( ) doesn’t take a const reference, but the call to g2( ) is OK.
Song Yi visit me in Shanghai
Song Yi came to Shanghai from Hangzhou today! I showed him around the city, the Huangpu River, the Jinmao Tower and  the Nanjing Road etc.
C++ Tips from Thinking in C++(1)
The following C++ grammar items are extracted from the book Thinking in C++. Most of them are often forgotten by myself. I will record here for my future quick reference.
GNU Automake tools Quick Reference(Autoconf)
Because of the diversity of the Unix system, the famous ‘configure’ script should be runned before compiling the source code on the system. The script check your system environment such as where your library locate and help you decide which part of the source code should be complied. This small simple qucik refrence is made by myself when I started to work as Software Engineer.
Continue reading “GNU Automake tools Quick Reference(Autoconf)”
Autoconf Macro Reference
This is an alphabetical list of each Autoconf macro used in this book, along with a description of what each does.
The documents is not made by me. I just extract it from the e-book written by Gary V. Vaughan.
Some commands that I often use in Emacs
Before the end of June, 2005. I never heard of `Emacs‘ despite graduating with a computer science degree. Thanks to Daniel who told me such an excellent Editor and who taught me lots of tricks.
Like new cufflinks and studs for Tuxedo and formal wear for sale
I am selling one of my cufflinks and studs set which is only used for once.
ã‚¿ã‚シード用カフス&スタッド(é»’) フリー 3800 円Â
2ã‹æœˆå‰ã«æ–°å“購入ã—ãŸã®ã§ã€ç¶ºéº—ãªçŠ¶æ…‹ã§ã™ã€‚ã™ããŠè²ã‚Šã§ãã¾ã™ã€‚連絡先 080 3916 1983
This cufflinks and studs set for your formal wear is used once only – so it deserves the name” almost like new”. (I can speak Japanese if we do local deal and meet up around Shibuya, Shinjuku area. Please contact me at 080 3916 1983)
This set is perfect for you at a black-tie event and most other men are poorly dressed in their tuxedos.  (Local deal in Tokyo is preferred, though international buyer is posisble but based on good mutual good faith if you trust me 🙂
Price is 3800 JPY (purchased originally at Rakuten at 5900 JPY, please see the link http://item.rakuten.co.jp/auc-tradhousefukiya/cs01/
å¦‚æžœä½ åœ¨ä¸å›½ã€‚ä½ å¯ä»¥é€šè¿‡ä»¥ä¸‹æ·˜å®é“¾æŽ¥è´ä¹°
http://trade.2.taobao.com/?spm=2007.3062033.10.8.gA8Qy0