TAILIEUCHUNG - HandBooks Professional Java-C-Scrip-SQL part 63

Tham khảo tài liệu 'handbooks professional java-c-scrip-sql part 63', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Usage The Any library resides in namespace boost. You use the class any to store values and the template function any_cast to subsequently retrieve the stored values. To use any include the header boost . The creation of an instance capable of storing any conceivable value is straightforward. boost any a To assign a value of some type is just as easy. a std string A string a 42 a Almost anything is acceptable to any However to actually do anything with the value contained in an any we need to retrieve it right For that we need to know the value s type. std string s boost any_cast std string a throws boost bad_any_cast. This obviously doesn t work because a currently contains a double any_cast throws a bad_any_cast exception. The following however does work. double d boost any_cast double a any only allows access to the value if you know the type which is perfectly sensible. These two elements are all you need to remember typewise for this library the class any for storing the values and the template function any_cast to retrieve them. Anything Goes Consider three classes A B and C with no common base class that we d like to store in a std vector. If there is no common base class it would seem we would have to store them as void right Well not any more pun intended because the type of any does not change depending on the type of the value it contains. The following code shows how to solve the problem. include iostream include string include utility include vector include boost class A public void some_function std cout A some_function n class B public void some_function std cout B some_function n class C public void some_function std cout C some_function n int main std cout Example of using any. n n std vector boost any store_anything A B C While we re at it let s add a few other things as well std string This is fantastic .

Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.