Time of Update: 2018-12-06
使用前需引用標頭檔#include <iomanip> cout.precision(x),setprecision(x)設定精度float f = 33.456; cout.precision(3);cout<<f<<endl;cout<<setprecision(3)<<f<<endl;// out:33.5 一些其它函數dec //置基數為10 相當於"%d" hex //置基數為16 相當於"%X" oct
Time of Update: 2018-12-06
//*********************************************************//條款一 類的成員函數指標變數之間可相互強轉//*********************************************************class A{};class B{};class C{};typedef void (A::*AFun)(void);typedef int (B::*BFun)(int, int);typedef void (C:
Time of Update: 2018-12-06
C#中可以用XmlDocument類操作Xml檔案例如要讀取如下Xml檔案1 <root> 2 <person name="WangYao"> 3 <age>25</age> 4 </person> 5 <person name="Jobs"> 6 <age>56</age> 7 </person> 8 </root> 程式如下
Time of Update: 2018-12-06
C++ cout 輸出 16, 8 , 2進位#include <iostream>#include <iomanip>#include <bitset>using std::bitset;using std::hex;using std::oct;using std::cout;using std::cin;using std::endl;int main(){ int
Time of Update: 2018-12-06
只談一個問題:head_common.S中__switch_data:.long init_thread_union + THREAD_START_SPinit_thread_union 使用ctags無法跳轉,且連結指令碼中沒有init_thread_union,使用grep搜尋下發現這是在arch/unicore/kernel/init_task.c3
Time of Update: 2018-12-06
一旦涉及多重繼承(multiple inheritance;MI):程式有可能從一個以上的base class繼承相同名稱(如函數、typedef等)。那會導致較多的歧義機會。例如:class BorrowableItem { public: void checkOut(); };class ElectronicGadet { private: bool checkOut() const; };class
Time of Update: 2018-12-06
假設程式涉及矩形。為了讓Rectangle對象儘可能小,可能把定義矩形的點放在一個輔助的struct內再讓Rectangle去指它:class Point{ public: Point(int x, int y); ... void setX(int newVal); void setY(int newVal); ... }; struct RectData{
Time of Update: 2018-12-06
複合(composition)是類型之間的一種關係,當某種類型的對象內含它種類型的對象,便是這種關係:class Address {...}; class PhoneNumber {...}; class Person { public: ... private: std::string name;//合成成分物 Address address;//合成成分物 PhoneNumber
Time of Update: 2018-12-06
預設情況下c++以by value 的方式傳遞對象(或來自)函數。函數參數是以實參的副本為初值,用函數獲得的也是函數傳回值的一個副本這些副本由對象的copy建構函式產出,這可能使得pass-by-value成為昂貴的操作:class Person { public: Person(); virtual ~Person(); protected: private: std::string name;
Time of Update: 2018-12-06
如果客戶企圖使用某個介面而卻沒有獲得他所預期的行為,這個代碼就不該通過編譯,如果代碼通過了編譯,它的作為就該是客戶所想要的。 class Date { public: Date(int month, int day, int year); ... };第一,以錯誤的次序傳遞參數:Date(30, 3, 1998);//應該是“3,30”而不是“30,3”第二,傳遞一個無效的月份或天數:(打岔一個鍵) Date(2,
Time of Update: 2018-12-06
轉型(casts)破壞了類型系統(type system)。可能導致任何類型的麻煩。c++提供四種新式轉型const_cast<T>(expression) //cast away the constnessdynamic_cast<T>(expression) //safe
Time of Update: 2018-12-06
template metaprogramming(模板元編程)是編寫template-based
Time of Update: 2018-12-06
重新定義一個繼承而來的non-virtual函數永遠都是錯誤的,本條款的討論限制在“帶有預設參數的virtual函數”。virtual函數是動態綁定的,而預設參數卻是靜態繫結。對象的所謂靜態類型,是它在程式中被聲明時所採用的類型。class Shape { public: enum ShapeColor {Red, Green, Blue}; virtual void draw(ShapeColor color = Red) const = 0;
Time of Update: 2018-12-06
有個class用來表現夾帶背景圖案的GUI菜單單,這個class用於多線程環境:class PrettyMenu{ public: ... void changeBackground(std::istream& imgSrc); ... private: Mutex mutex; Image* bgImage; int imageChanges; };
Time of Update: 2018-12-06
假設一個函數用來揭示處理常式的優先權int priority();另一個函數用來在動態分配的Widget上進行某些帶有優先權的處理:void processWidget(std::tr1::shared_ptr<Widget> pw, int priority);考慮調用processWidget:processWidget(new Widget,
Time of Update: 2018-12-06
c++中public繼承視為is-a關係。現在看private繼承:class Person{...}; class Student: private Person {...}; void eat(const Person& p); void study(const Student& s);Person p; Student s; eat(p); eat(s); //錯誤! 難道學生不是人?!顯然private繼承不是is-
Time of Update: 2018-12-06
stl主要由“用以表現容器、迭代器和演算法”的template構成,但也覆蓋若干工具性的templates,其中一個名為advance,將某個迭代器移動某個給定距離:template<typename IterT, typename DistT> void advance(IterT& iter, DistT d);
Time of Update: 2018-12-06
設計優秀的classes和是一項艱巨的工作,因為設計好的types是一項艱巨的工作。設計出至少像c++內建類型一樣好的classes。幾乎每一個class都要求面對以下提問,回答往往導致你的設計規範:1,新type的對象應該如何建立和銷毀?包括構造和解構函式,記憶體配置和釋放函數(operator new, operator delete,operator new[], operator
Time of Update: 2018-12-06
當operator new無法滿足某一記憶體配置需求時,會拋出異常。再拋出異常以反映一個未獲滿足的記憶體需求之前,它會先調用客戶指定的錯誤處理函數,new-handler。為了指定這個“用以處理記憶體不足”的函數,客戶必須調用set-new-handler,那是聲明於<new>的一個標準函數庫函數:namespace std{ typedef void (*new_handler)(); new_handler
Time of Update: 2018-12-06
inline函數,可以調用它們而又不需蒙受函數調用所招致的額外開銷當你inline某個函數,或許編譯器就因此又能力對它(函數本體)執行語境相關最佳化。然而,inline函數背後的整體觀念是,將“對此函數的每一個調用”都已函數本體替換之,這樣做可能增加你的目標碼(object