搜尋此網誌

2010年4月25日 星期日

肚皮舞者


在一個偶然的機會,有機會目睹朋友的肚皮舞表演,特拍照留念.
但,由於天色昏暗,距離又遠,又受限於器材...
只能透過高ISO的方式避免手震..但拍出來雜訊也就可想而知了..

拍舞者真的不容易,舞者的動感的韻味真的很難掌握.
不過有了這次經驗,我相信我下次一定可以拍得更好些.
只是....下次的機會不知道何時才會來到....

2010年4月18日 星期日

走向共和


最近看了一部大陸的影集"走向共和",劇情背景為自清朝慈禧太后歸政光緒皇帝,至民初袁世凱稱帝失敗,約廿年間的中國歷史,包括洋務運動、中日甲午戰爭、戊戌變法、庚子拳亂、庚子後新政、辛亥革命、民初政治等史事
這部影集刻畫清末史實非常細膩,但凡涉及民國初年政治敏感的劇情卻總是輕描淡寫的帶過,實有些許遺憾.也許是居於現實環境的考量(畢竟是大陸拍攝的影集阿),但瑕不掩瑜,整體說來是一部值得一看的佳作.
......只看看到最後一集,歌頌中國共產黨的文字..讓我有一種錯亂的感覺....怎麼跟我國中時學到歷史不太一樣阿?......哈!真不知我們是被國民黨騙了,還是他們被共產黨唬了....

終於又有一款電玩破關了

這次破關是GC的遊戲 "路易的鬼屋",雖是八百年前的老遊戲
但...還是要紀念一下拉..因為我能破關的遊戲真的不多啦.

2010年4月7日 星期三

Returning values by value, reference, and address

Return by value


Return by value is the simplest and safest return type to use. When a value is returned by value, a copy of that value is returned to the caller. As with pass by value, you can return by value literals (eg. 5), variables (eg. x), or expressions (eg. x+1), which makes return by value very flexible.


Another advantage of return by value is that you can return variables (or expressions) that involve local variables declared within the function. Because the variables are evaluated before the function goes out of scope, and a copy of the value is returned to the caller, there are no problems when the variable goes out of scope at the end of the function.


1.int DoubleValue(int nX)
2.{
3.    int nValue = nX * 2;
4.    return nValue; // A copy of nValue will be returned here
5.} // nValue goes out of scope here


Return by value is the most appropriate when returning variables that were declared inside the function, or for returning function arguments that were passed by value. However, like pass by value, return by value is slow for structs and large classes.


Return by reference


Just like with pass by reference, values returned by reference must be variables (you can not return a reference to a literal or an expression). When a variable is returned by reference, a reference to the variable is passed back to the caller. The caller can then use this reference to continue modifying the variable, which can be useful at times. Return by reference is also fast, which can be useful when returning structs and classes.


However, returning by reference has one additional downside that pass by reference doesn’t — you can not return local variables to the function by reference. Consider the following example:


1.int& DoubleValue(int nX)
2.{
3.    int nValue = nX * 2;
4.    return nValue; // return a reference to nValue here
5.} // nValue goes out of scope here


See the problem here? The function is trying to return a reference to a value that is going to go out of scope when the function returns. This would mean the caller receives a reference to garbage. Fortunately, your compiler will give you an error if you try to do this.


Return by reference is typically used to return arguments passed by reference to the function back to the caller. In the following example, we return (by reference) an element of an array that was passed to our function by reference:


01.// This struct holds an array of 25 integers
02.struct FixedArray25
03.{
04.    int anValue[25];
05.};
06. 
07.// Returns a reference to the nIndex element of rArray
08.int& Value(FixedArray25 &rArray, int nIndex)
09.{
10.    return rArray.anValue[nIndex];
11.}
12. 
13.int main()
14.{
15.    FixedArray25 sMyArray;
16. 
17.    // Set the 10th element of sMyArray to the value 5
18.    Value(sMyArray, 10) = 5;
19. 
20.    cout << sMyArray.anValue[10] << endl;
21.    return 0;
22.}


This prints:


5

When we call Value(sMyArray, 10), Value() returns a reference to the 10th element of the array inside sMyArray. main() then uses this reference to assign that element the value 5.


Although this is somewhat of a contrived example (because you could access sMyArray.anValue directly), once you learn about classes you will find a lot more uses for returning values by reference.


Return by address


Returning by address involves returning the address of a variable to the caller. Just like pass by address, return by address can only return the address of a variable, not a literal or an expression. Like return by reference, return by address is fast. However, as with return by reference, return by address can not return local variables:


1.int* DoubleValue(int nX)
2.{
3.    int nValue = nX * 2;
4.    return &nValue; // return nValue by address here
5.} // nValue goes out of scope here


As you can see here, nValue goes out of scope just after its address is returned to the caller. The end result is that the caller ends up with the address of non-allocated memory, which will cause lots of problems if used. This is one of the most common programming mistakes that new programmers make. Many newer compilers will give a warning (not an error) if the programmer tries to return a local variable by address — however, there are quite a few ways to trick the compiler into letting you do something illegal without generating a warning, so the burden is on the programmer to ensure the address they are returning will be to a valid variable after the function returns.


Return by address is often used to return newly allocated memory to the caller:


01.int* AllocateArray(int nSize)
02.{
03.    return new int[nSize];
04.}
05. 
06.int main()
07.{
08.    int *pnArray = AllocateArray(25);
09.    // do stuff with pnArray
10. 
11.    delete[] pnArray;
12.    return 0;
13.}


Conclusion


Most of the time, return by value will be sufficient for your needs. It’s also the most flexible and safest way to return information to the caller. However, return by reference or address can also be useful, particularly when working with dynamically allocated classes or structs. When using return by reference or address, make sure you are not returning a reference to, or the address of, a variable that will go out of scope when the function returns!




摘自: http://www.learncpp.com/cpp-tutorial/74a-returning-values-by-value-reference-and-address