classDuck defquack puts "Quaaaaaack!" end deffeathers puts "The duck has white and gray feathers." end end classPerson defquack puts "The person imitates a duck." end deffeathers puts "The person takes a feather from the ground and shows it." end end defin_the_forestduck duck.quack duck.feathers end defgame donald = Duck.new john = Person.new in_the_forest donald in_the_forest john end game
在C++98/03時期,有這麼一條規則Every C++ expression is either an lvalue or an rvalue.,Lvalue是在運算過後留下來的續存物件,而Rvalue是運算過後生命期就結束的臨時物件。 殂此之外,C++98/03裡還有一條規則A function call is an lvalue if and only if the result type is a reference
structAdd2 { intoperator()(int a, int b) { return a + b; } }; intsub2(int a, int b){ return a - b; } template <typename T> intoperate(int a, int b, T callback) { return callback(a, b); } operate(3, 2, Add2()); operate(3, 2, sub2);
之後有人想要將class member method跟static class method也包裝成Functor的形式,有Loki跟Boost兩大主流為主。這裡就不多談了,在C++0x中也將boost的bind跟function列入TR1裡,可以很方便的產生Functor。
std::forward_list is a container that supports fast insertion and removal of elements from anywhere in the container. Fast random access is not supported. It is implemented as singly-linked list and essentially does not have any overhead compared to its implementation in C. Compared to std::list this container provides more space efficient storage when bidirectional iteration is not needed.