通過分層來體現 “有一個” 或 “用…來實現”

   條款40: 通過分層來體現 "有一個" 或 "用...來實現" 使某個類的對象成為另一個類的資料成員,從而實現將一個類構築在另一個類之上,這一過程稱為 "分層"(Layering)。例如:class Address { ... };           // 某人居住之處class PhoneNumber { ... };class Person {public:  ...private:  string name;                   // 下層對象  Address

區分繼承和模板

   條款41: 區分繼承和模板 考慮下面兩個設計問題:·

CEdit & CRichEdit 提示

.設定edit唯讀屬性    方法一:                m_edit1.SetReadOnly(TRUE);    方法二:                ::SendMessage(m_edit1.m_hWnd, EM_SETREADONLY, TRUE, 0);2.判斷edit中游標狀態並得到選中內容(richedit同樣適用)        int nStart, nEnd;        CString strTemp;       

利用 typename 消除歧義

template<class T>class Ptr{public: typedef T Emp; void insert(Emp a){     value += a; } void show() {  std::cout<<"the result is "<<value<<"/n"; } Ptr():value( T()){}private: Emp value;};template<class T>void fill(T

簡單的成員模板執行個體

template<class T>class SList {public:     SList():head_(0),size(0) {}  template<class In> SList(In first, In last); void push_front(const T& val); void pop_front(); T front()const; void reverse(); bool empty()const; void show()const; 

algorithm 非修改性序列操作源碼

template<class _II, class _Fn> inline _Fn for_each(_II _F, _II _L, _Fn _Op) {for (; _F != _L; ++_F)  _Op(*_F); return (_Op); }  // TEMPLATE FUNCTION findtemplate<class _II, class _Ty> inline _II find(_II _F, _II _L, const _Ty&

分離檔案中的單詞寫到另一檔案中

set<char*>Word;ofstream ofs("c://word.txt",ios_base::app);//按給定的標點來拆分單詞void SplitString(const string&str){ size_t nlen = str.length(); char *strDest = new char[nlen + 1]; strcpy(strDest,str.c_str()); char *flag =",.!:<>?"; char *p =

採用template消除歧義

template<class T>class Test {public: template<class Other> class show { public:  typedef Test<Other> other; };};template<typename T, class A = std::allocate<T> >class SList{   //.... struct Node {  //.... }; //typedef A:

簡單的單鏈表操作

#include <iostream>using namespace std;template<class T>class node{public: node():next(NULL) {} node(const T&value, node<T> *nextValue = NULL) : value_(value),next(nextValue) {} T getValue() {  return value_; }public: node<T&

讓operator=返回*this的引用

c++的設計者bjarne stroustrup下了很大的功夫想使使用者自訂類型儘可能地和固定類型的工作方式相似。這就是為什麼你可以重載運算子,寫類型轉換函式(見條款m5),控制賦值和拷貝建構函式,等等。他做了這麼多努力,那你最少也該繼續做下去。讓我們看看賦值。用固定類型的情況下,賦值操作可以象下面這樣鏈起來:int w, x, y, z;w = x = y = z = 0;所以,你也應該可以將使用者自訂類型的賦值操作鏈結起來:string w, x, y, z;// string是由標準c++

Item 45. BOOL

Item 45. BOOLDifficulty: 7Do we really need a builtin bool type? Why not just emulate it in the existing language? This Item reveals the answer.Besides wchar_t (which was a typedef in C), bool is the only builtin type to be added to C++ since the

針對類型資訊的特化

類模板顯式特化和局部特化通常用於產生主類模板的一些版本,這些版本根據具體的模板模板實參或模板實參的類定製而成。然而,這些語言特性常常也被以相反的樣式使用,即,不是基於類型的屬性產生特化版本,而是從一個特化版本中推匯出類型的屬性。例:template<class T>struct IsInt  //T 不是一個 int{ enum { result = false };};template<>struct IsInt<int> //除非T

Item 46. Forwarding Functions

Item 46. Forwarding FunctionsDifficulty: 3What's the best way to write a forwarding function? The basic answer is easy, but we'll also learn about a subtle change to the language made shortly before the standard was finalized.Forwarding functions

Item 44. Casts

I l@ve RuBoard Item 44. CastsDifficulty: 6How well do you know C++'s casts? Using them well can greatly improve the reliability of your code.The new-style casts in standard C++ offer more power and safety than the old-style (C-style) casts. How

關於存取控制問題(publc protected private)

類中的一個成員可以是:private  protected public1.如果一個成員是private ,它的名字將只能由其聲明所在類的成員函數和友元使用2.如果一個成員是protected,它的名字只能由其聲明所在類的成員函數和友元,以及由該類的衍生類別的成員函數及友元使用3.如果一個成員是public ,它的名字可以由任何函數使用衍生類別與基類的繼承許可權(public protected

Why is the size of an empty class not zero?

To ensure that the addresses of two different objects will be different. For the same reason, "new" always returns pointers to distinct objects. Consider:class Empty { };void f(){Empty a, b;if (&a == &b) cout << "impossible: report

解構函式裡對指標成員調用delete

大多數情況下,執行動態記憶體分配的的類都在建構函式裡用new分配記憶體,然後在解構函式裡用delete釋放記憶體。最初寫這個類的時候當然不難做,你會記得最後對在所有建構函式裡分配了記憶體的所有成員使用delete。然而,這個類經過維護、升級後,情況就會變得困難了,因為對類的代碼進行修改的程式員不一定就是最早寫這個類的人。而增加一個指標成員意味著幾乎都要進行下面的工作:    ·在每個建構函式裡對指標進行初始化。對於一些建構函式,如果沒有記憶體要分配給指標的話,指標要被初始化為0(即null

Can I stop people deriving from my class?

Yes, but why do you want to? There are two common answers: for efficiency: to avoid my function calls being virtual for safety: to ensure that my class is not used as a base class (for example, to be sure that I can copy objects without fear of

Item 42. Variable Initialization is it?

This first problem highlights the importance of understanding what you write. Here we have four simple lines of code, no two of which mean the same thing, even though the syntax varies only slightly.What is the difference, if any, between the

map 簡單用法

#include<iostream>#include<map>#include<string>#include<algorithm>using namespace std;struct Data{   Data(): cout(0),sum(0) {}   int cout;   double sum;};typedef map<string,Data> M;void CollData(M &m){   string name;

總頁數: 61357 1 .... 15642 15643 15644 15645 15646 .... 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.