目錄(原創博客,版權所有,轉載請注明出處 http://www.cnblogs.com/feng-sc)
2.2、C++實現效果與C#效果對比(熟悉C#屬性讀者可從此小節開始)
3.1、property_rw介紹
3.2、property_r、property_w介紹
1、概述
在程序員的行業里,有一個有趣的說法,但我覺得這個說法在其他行業也同樣適用:行業的一流高手都在制定標准和概念,二流高手都在實現標准和概念,三流高手的都在使用標准。
我們今天來做一次二流的高手做的事情:1、為c++引入一個概念標准;2、用c++實現這個概念。
2、C#屬性的概念
本想略過對C#屬性的介紹,但考慮到讀者可能沒使用過C#,也不知道C#的屬性到底有什么用,因此還是稍微從使用的角度先解釋一下C#中屬性的概念(如果您對屬性已經足夠了解,可直接跳過本章或直接從下面第2點的“C++實現效果與C#效果對比”開始)。
一下內容包含如下部分:
1、簡單示例代碼介紹C#中的屬性;
2、C++實現效果與C#效果對比;
1、簡單示例代碼介紹C#中的屬性
我們來看第一段C#代碼:
//示例代碼1.0 http://www.cnblogs.com/feng-sc/p/5742689.html
class TestDemo { public String Name { get; set; } } class Program { static void Main(string[] args) { TestDemo test = new TestDemo(); test.Name = "test"; System.Console.WriteLine(test.Name); } }
上面代碼中,我們創建了一個String類型的屬性Name,這個時候我們,我們的屬性其實跟一個成員變量沒啥區別,只是一個成員變量罷了。
另外,還需要讀者了解的是,屬性里的get和set分別控制屬性的讀寫權限:
1、如果只有get,那么Name為只讀模式;
2、如果只有set,那么Name為只寫模式;
只讀模式示例代碼:
1 //示例代碼1.0 http://www.cnblogs.com/feng-sc/p/5742689.html 2 class TestDemo 3 { 4 public String Name 5 { 6 get; 7 } 8 } 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 TestDemo test = new TestDemo(); 14 test.Name = "test";//只讀屬性,賦值不允許,編譯出錯
15 System.Console.WriteLine(test.Name); 16 } 17 }
只寫模式示例代碼:
1 //示例代碼1.0 http://www.cnblogs.com/feng-sc/p/5742689.html 2 class TestDemo 3 { 4 public String Name 5 { 6 set; 7 } 8 } 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 TestDemo test = new TestDemo(); 14 test.Name = "test"; 15 System.Console.WriteLine(test.Name); //只寫模式,test.Name取值不允許,編譯失敗 16 } 17 }
我們再來看一個復雜點的例子:
1 //示例代碼1.0 http://www.cnblogs.com/feng-sc/p/5742689.html 2 class TestDemo 3 { 4 public String Name 5 { 6 get 7 { 8 return _name; 9 } 10 set 11 { 12 _name = value; 13 TestWork(); 14 } 15 } 16 public void TestWork() 17 { 18 System.Console.WriteLine(_name); 19 } 20 21 private String _name; 22 } 23 class Program 24 { 25 static void Main(string[] args) 26 { 27 TestDemo test = new TestDemo(); 28 test.Name = "test"; 29 } 30 }
以上代碼中,我們運行到第28行的時候,會進入到第10行,最終運行結果打印出“test”。
是的,其實很簡單,如果沒有屬性,我們完全可以用一個私有的成員函數,然后為每個成員函數添加一個get和set的方法(java下的通常做法),如下代碼中的TestDemo完全能實現上面代碼中屬性的做法:
1 //示例代碼1.0 http://www.cnblogs.com/feng-sc/p/5742689.html 2 class TestDemo 3 { 4 public String GetName() 5 { 6 return _name; 7 } 8 public void SetName(String value) 9 { 10 _name = value; 11 TestWork(); 12 } 13 public void TestWork() 14 { 15 System.Console.WriteLine(_name); 16 } 17 18 private String _name; 19 }
……
OK,那為什么要有屬性呢?
其實追求的真是一種書寫方法吧,屬性能讓我們像訪問成員變量一樣訪問直接賦值和取值,同時,在賦值和取值的時候,能根據需求調用相應的內部實現函數。
2、C++實現效果與C#效果對比
請讀者對比下左右兩邊的屬性Name的定義方式,C#里面的get和set方法在C++11實現中,使用了property_getter和property_setter實現,property_getter和property_setter中代碼的實現方式與C#均一致。
OK,看完屬性效果比較,如果您覺得對你的胃口,那么可以繼續往下看,下面會有關於proerty_rw更詳細的說明。
3、如何使用C++11實現C#屬性的概念模式
本章我們將一步步介紹C++11實現屬性三種形式分別是:
1、property_rw :對應C#的讀寫模式(get和set均有)
2、property_r :對應C#的只讀模式(沒有set)
3、property_w :對應C#的只寫模式(沒有get)
1、property_rw介紹
property_rw的實現代碼很簡單,但需要大家對C++11中的std::function和lamda表達式有所了解,如果您不是很了解或在下面介紹中覺得難懂,可以先看看我之前寫的關於C++11的總結文章:【干貨】C++11常用特性的使用經驗總結,對您理解本章內容會有幫助。
proerty_rw源碼:
1 //示例代碼1.0 http://www.cnblogs.com/feng-sc/p/5742689.html 2 #include<functional> 3 #define property_setter(variableType) [&](variableType value) 4 #define property_getter(variableType) [&]()->variableType 5 template <typename ValueType> 6 class property_rw 7 { 8 public: 9 typedef std::function<void(ValueType value)> Setter; 10 typedef std::function<ValueType(void)> Getter; 11 explicit property_rw(Setter setter, Getter getter) : m_setter(setter), m_getter(getter) {} 12 property_rw& operator=(ValueType value) 13 { 14 this->value = value; 15 m_setter(this->value); 16 return *this; 17 } 18 property_rw& operator=(const property_rw & instance) 19 { 20 this->value = instance.m_getter(); 21 m_setter(this->value); 22 return *this; 23 } 24 operator ValueType() 25 { 26 return m_getter(); 27 } 28 private: 29 ValueType value; 30 Setter m_setter; 31 Getter m_getter; 32 };
上面代碼我們可以看出,property_rw是一個模板類,ValueType為屬性的類型名,因此大家可以想到,我們的屬性其實是一個類對象。
因此,我們來看一個最簡單的使用示例:
//示例代碼1.0 http://www.cnblogs.com/feng-sc/p/5742689.html
#include<string> #include"property.h" class TestDemo { public: property_rw<std::string> Name; };
需要注意:#include<string>需要放在#include"property.h"之前,同理,其他非基本數據類型引用頭文件均需如此,這主要是由於proerty.h內部宏定義的property_setter和property_getter所導致,要保持屬性的書寫風格,目前暫時沒有很好的解決這個問題。
上面的代碼無法編譯通過,但我們先看看形式,我們定義了一個string類型的屬性Name。
從proerty_rw的構造函數看explicit property_rw(Setter setter, Getter getter),我們定義這個屬性,需要給這個屬性賦值兩個參數,分別為Setter和Getter。Getter和Setter類型分別為std::function定義的兩個可執行對象類型。在【干貨】C++11常用特性的使用經驗總結文章中,我們介紹了std::function定義的可執行對象可以有三種形式的賦值,分別是:
1、同形式(返回值和參數相同)函數指針;
2、同形式的類成員函數;
3、同形式的lamda表達式函數;
為了統一外面的使用形式,我們使用宏的方式定義(property_rw源碼第3、4行)property_getter和proerty_setter,該宏定義其實是限制外部使用lamda表達式方式(當然,這沒能從編譯源頭限制,其實外部還是可以使用第1、2兩種方式)。我們來看下property_getter和proerty_setter的定義:
#define property_setter(variableType) [&](variableType value) //定義lamda表達式的頭部,[&]表示對定義范圍內的變量取值權限為引用形式,參數為variableType
#define property_getter(variableType) [&]()->variableType //property_getter的lamda表達式返回值為variableType
明白了property_getter和proerty_setter的定義,我們來看下Name的初始化:
//示例代碼1.0 http://www.cnblogs.com/feng-sc/p/5742689.html
class TestDemo { public: property_rw<std::string> Name = property_rw<std::string>( property_setter(std::string) { }, property_getter(std::string) { return "test"; } ); };
這個時候我們來看Name的初始化就非常清晰了,其實就是給property_rw構造函數傳遞了兩個lamda表達式定義的函數,經過我們的宏封裝,使我們的屬性使用風格看起來與C#形式很像。
我們再來看看property_rw中的函數,控制讀取屬性的函數:
//示例代碼1.0 http://www.cnblogs.com/feng-sc/p/5742689.html
property_rw& operator=(ValueType value) { this->value = value; m_setter(this->value); return *this; } property_rw& operator=(const property_rw & instance) { this->value = instance.m_getter(); m_setter(this->value); return *this; }
加入我們現在有兩個屬性string類型的屬性,名字分別為Name1,Name2,那么什么時候會調用上面兩個函數呢?
TestDemo test; test.Name1 = "test"; //調用第一個operator=()函數 test.Name2 = test.Name1; //調用第二個operator=()函數
property_rw中控制寫熟悉的函數:
operator ValueType() { return m_getter(); }
示例:
TestDemo test; test.Name = "test"; std::string str = test.Name //調用寫屬性控制函數
小結:
property_rw的模板類實現簡單吧,沒有太多的邏輯代碼,都是普通的類設計而已,關鍵需要大家有一些編碼技巧。
2、property_r、property_w介紹
property_r和property_w就更加簡單了,我么只需要把property_rw中的控制讀或控制寫的屬性函數去掉,就可以變成只讀或只寫的屬性類型。
property_r源碼:
//示例代碼1.0 http://www.cnblogs.com/feng-sc/p/5742689.html
template <typename ValueType> class property_r { public: typedef std::function<void(ValueType value)> Setter; typedef std::function<ValueType(void)> Getter; explicit property_r(Getter getter) : m_getter(getter) {} operator ValueType() { return m_getter(); } private: ValueType value; Getter m_getter; };
property_w源碼:
//示例代碼1.0 http://www.cnblogs.com/feng-sc/p/5742689.html
template <typename ValueType> class property_w { public: typedef std::function<void(ValueType value)> Setter; explicit property_w(Setter setter) : m_setter(setter) {} property_w& operator=(ValueType value) { this->value = value; m_setter(this->value); return *this; } property_w& operator=(const property_w & instance) { this->value = instance.m_getter(); m_setter(this->value); return *this; } private: ValueType value; Setter m_setter; };
property_r和property_w就沒有什么可以介紹的了,基本和property_rw類似。
3、完整屬性測試代碼介紹
下面是一個比較完整的測試例子:
//示例代碼1.0 http://www.cnblogs.com/feng-sc/p/5742689.html
#include<iostream> #include<string> #include"property.h" class TestDemo { public: property_rw<std::string> Name = property_rw<std::string>( property_setter(std::string) { m_rw_name = value; }, property_getter(std::string) { return m_rw_name; } ); property_r<std::string> ReadOnlyName = property_r<std::string>( property_getter(std::string) { return m_readonly_name; } ); property_w<std::string> WriteOnlyName = property_w<std::string>( property_setter(std::string) { m_writeonly_name = value; TestWork(); } ); void TestWork() { std::cout <<"TestWork()::m_writeonly_name--"<< m_writeonly_name << std::endl; } private: std::string m_rw_name; std::string m_readonly_name = "I m read only name"; std::string m_writeonly_name = ""; }; int main() { TestDemo test; test.Name = "This is test name!"; std::string str = test.Name; std::string readonly = test.ReadOnlyName; std::cout << "Test read and write,Name:" << str << std::endl; std::cout << "Test readonly, msg:" << readonly << std::endl; test.WriteOnlyName = "This is write only property!"; }
運行結果:
4、總結
本文我們首先介紹了C#屬性概念,以及為什么使用屬性,再一步一步引出我們如何使用C++11實現C#屬性的效果。整篇文章所述沒有太多邏輯的問題,單純用我們所熟知的C++11知識加上我們的一些編碼技巧,最后我們用很普通的做法實現了我們想要的效果。編程有時候給人的感覺就是這樣,先要有一個好的概念(如本文中的屬性概念),然后想辦法用我們熟知的知識需完美地把它實現,這會讓你蠻有成就感!