泛型程式設計基礎

1。C++ 範本提供了編譯期多態的能力,請解釋這名話     當我們在面象對向的世界中考慮多態的時候,所考慮的運行時多態,這種能力來自虛函數。基類建立一個介面“合約”,該“合約”由一系統虛函數構成,衍生類別可以從基類中進行派生,並在不違反合約所蘊涵的語義的前提下重寫基類的虛函數 。這樣一來,那些期望使用基類對象(通過指標或引用來持用基類對象)來工作的代碼使用衍生類別同樣能夠運行很好。       

雙鏈表基本操作

1.在頭接點插入指定的值template<class T>void InsertD(dnode<T>* &front,const T& value){ dnode<T>* prev = front->prev, *curr = front; dnode<T>* newNode = new dnode<T>(value); newNode->prev = prev; newNode->next =

為什麼不特化函數模板

1. C++ 中主要有哪兩種形式的模板,它們分別如何進行特化?    C++中有類模板與函數模板之分。這兩種模板的工作方式並不完全一樣,最明顯的區別在於重載,普通C++類是不能重載的,因此類模板也不能夠重載。另一方面,普通的C++函數如果名字相同(且函數簽名不同)就會發生重載,因而函數也允許重載。

Item 35. Memory Management part 1

How well do you know memory? What different memory areas are there?This problem covers basics about C++'s main distinct memory stores. The following problem goes on to attack some deeper memory management questions in more detail.C++ has several

Why are the standard containers so slow?

They are not. Probably "compared to what?" is a more useful answer. When people complain about standard-library container performance, I usually find one of three genuine problems (or one of the many myths and red herrings):I suffer copy overhead I

千萬不要返回局部對象的引用,也不要返回函數內部用new初始化的指標的引用

本條款聽起來很複雜,其實不然。它只是一個很簡單的道理,真的,相信我。先看第一種情況:返回一個局部對象的引用。它的問題在於,局部對象 ----- 顧名思義 ----

What’s wrong with arrays?

In terms of time and space, an array is just about the optimal construct for accessing a sequence of objects in memory. It is, however, also a very low level data structure with a vast potential for misuse and errors and in essentially all cases

algorithm 修改性的序列操作源碼

// TEMPLATE FUNCTION transform WITH UNARY OPtemplate<class _II, class _OI, class _Uop> inline _OI transform(_II _F, _II _L, _OI _X, _Uop _U) {for (; _F != _L; ++_F, ++_X)  *_X = _U(*_F); return (_X); }  // TEMPLATE FUNCTION transform WITH

確保非局部靜態對象在使用前被初始化

   確保非局部靜態對象在使用前被初始化 大家都是成年人了,所以用不著我來告訴你們:使用未被初始化的對象無異於蠻幹。事實上,關於這個問題的整個想法會讓你覺得可笑;建構函式可以確保對象在建立時被初始化,難道不是這樣嗎?唔,是,也不是。在某個特定的被編譯單元(即,源檔案)中,可能一切都不成問題;但如果在某個被編譯單元中,一個對象的初始化要依賴於另一個被編譯單元中的另一個對象的值,並且這第二個對象本身也需要初始化,事情就會變得更複雜。例如,假設你已經寫了這樣一個程式庫,它提供一個檔案系統的抽象,其中可

VC製作系統托盤程式

Windows作業系統中的某些程式運行時不顯示運行視窗,只在工作列上顯示一個表徵圖,表示程式正在運行,使用者可以通過滑鼠與應用程式互動,比如金山毒霸等應用程式,我們有時也需要編製一些僅在後台啟動並執行類似程式,為了不干擾前景程式的運行介面和不顯示不必要的視窗,應使程式運行時的主視窗不可見。同時將一個表徵圖顯示在工作列右端靜態通告區中並響應使用者的滑鼠動作。下面介紹Visual C++開發這類程式的設計方法。   一、隱藏程式的主視窗

Item 38. Object Identity

"Who am I, really?" This problem addresses how to decide whether two pointers really refer to the same object.The "this != &other" test (illustrated below) is a common coding practice intended to prevent self-assignment. Is the condition

Item 40. Object Lifetimes part 1

"To be, or not to be…" When does an object actually exist? This problem considers when an object is safe to use.Critique the following code fragment.void f() { T t(1); T& rt = t; //--- #1: do something with t or rt --- t.~T(); new (&t)

Item 36. Memory Management part 2

Are you thinking about doing your own class-specific memory management, or even replacing C++'s global new and delete? First, try this problem on for size.The following code shows classes that perform their own memory management. Point out as many

Item 39. Automatic Conversions

Automatic conversions from one type to another can be extremely convenient. This Item covers a typical example to illustrate why they're also extremely dangerous.The standard C++ string has no implicit conversion to a const char*. Should it?It is

vector 的使用

size/resize  以及 capacity/reserve 之間的區別size 取得 vector 中元素的個數,resize 重新分配空間,並且將分配的空間初始化為0,capacity : return number of elements for while memory has been allocatedreserve:Allocates elements for vector to ensure a minimum size,preserving its contents if

Why use sort() when we have “good old qsort()”?

To a novice,qsort(array,asize,sizeof(elem),elem_compare);looks pretty weird, and is harder to understand thansort(vec.begin(),vec.end());To an expert, the fact that sort() tends to be faster than qsort() for the same elements and the same comparison

在operator=中檢查給自己賦值的情況

做類似下面的事時,就會發生自己給自己賦值的情況:class x { ... };x a;a = a;                     // a賦值給自己這種事做起來好象很無聊,但它完全是合法的,所以看到程式員這樣做不要感到絲毫的懷疑。更重要的是,給自己賦值的情況還可以以下面這種看起來更隱形形式出現:a =

避免返回內部資料的控制代碼

請看物件導向世界裡發生的一幕:對象a:親愛的,永遠別變心!對象b:別擔心,親愛的,我是const。然而,和現實生活中一樣,a會懷疑,"能相信b嗎?" 同樣地,和現實生活中一樣,答案取決於b的本性:其成員函數的組成結構。假設b是一個const string對象:class string {public:  string(const char *value);        // 具體實現參見條款11  ~string();                        //

爭取使類的介面完整並且最小

類的使用者介面是指使用這個類的程式員所能訪問得到的介面。典型的介面裡只有函數存在,因為在使用者介面裡放上資料成員會有很多缺點(見條款20)。哪些函數該放在類的介面裡呢?有時這個問題會使你發瘋,因為有兩個截然不同的目標要你去完成。一方面,設計出來的類要易於理解,便於使用,易於實現。這意味著函數的數量要儘可能地少,每一個函數都完成各自不同的任務。另一方面,類的功能要強大,要方便使用,這意味著要不時增加函數以提供對各種通用功能的支援。你會怎樣決定哪些函數該放進類裡,哪些不放呢?試試這個建議:類介面的目

避免這樣的成員函數:其傳回值是指向成員的非const指標或引用,但成員的訪問級比這個函數要低

使一個成員為private或protected的原因是想限制對它的訪問,對嗎?勞累的編譯器要費九牛二虎之力來確保你設定的訪問限制不被破壞,對不對?所以,寫個函數來讓使用者隨意地訪問受限的成員沒多大意義,對不對?如果你確實認為有意義,那麼請反覆閱讀本段,直到你不這樣認為為止。實際編程中很容易違反這條規則,下面是個例子:class address { ... };           // 某人居住在此class person {public:  address&

總頁數: 61357 1 .... 15641 15642 15643 15644 15645 .... 61357 Go to: 前往

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.