【C++】由指標常量和常量指標引發的問題

來源:互聯網
上載者:User

今天在寫一個C++程式的時候,要用到STL的set容器,而且我想讓它利用自訂的排序準則來實現自動排序。而且set中元素類型是指向自訂的結構體的指標。結果運行總是出錯。

下面先貼出起初的代碼:

#include <iostream>#include <set>using namespace std;typedef struct test_t{int a;int b;}test;class PersonalCriterion{public:bool operator()(const test* &obj1,const test* &obj2){return obj1->a < obj2->a;}};int main(){set<test*,PersonalCriterion> myset;test* obj1 = new test();obj1->a = 10;obj1->b = 9;test* obj2 = new test();obj2->a = 5;obj2->b = 9;myset.insert(obj1);myset.insert(obj2);return 0;}

結果在VS2008中編譯運行出錯,錯誤資訊如下:

顯然問題出在這個自訂的仿函數排序準則這裡。那麼究竟是什麼問題呢?錯誤資訊顯示這裡丟失了限定符,即const。但是我這裡參數上明明有const呀,疑惑!

再次仔細看輸出的錯誤資訊,不能將參數一從類型1轉化到類型2.這兩個類型有什麼區別呢?答案是const的位置不同:指標常量和常量指標的區別!在有指標的地方,const放在不同的位置會導致完全不同的結果。前一種類型中,const是來修飾指標本身的,即指標常量,它指向的對象確定了之後就不能修改了,即不能修改使得它指向另外一個對象。而第二種類型,const是來修飾指標所指向的類型的,即常量指標,指標本身不是const的,可以修改指向別的對象。但是指標所指向的地址處的內容卻不能修改,它是const的。

接下來就可以對代碼進行修改了。為了避免出現上述的問題,我們用typedef重新定義了指向自訂結構體的指標類型:

這樣就不會出現原來的問題了。修改正確的代碼如下:

#include <iostream>#include <set>using namespace std;typedef struct test_t{int a;int b;}test;typedef test* test_ptr;class PersonalCriterion{public:bool operator()(const test_ptr &obj1,const test_ptr &obj2){return obj1->a < obj2->a;}};int main(){set<test_ptr,PersonalCriterion> myset;test_ptr obj1 = new test();obj1->a = 10;obj1->b = 9;test_ptr obj2 = new test();obj2->a = 5;obj2->b = 9;test_ptr obj3 = new test();obj3->a = 20;obj3->b = 48;test_ptr obj4 = new test();obj4->a = 1;obj4->b = 2;test_ptr obj5 = new test();obj5->a = 3;obj5->b = 39;myset.insert(obj1);myset.insert(obj2);myset.insert(obj3);myset.insert(obj4);myset.insert(obj5);set<test_ptr,PersonalCriterion>::iterator iter;for (iter = myset.begin();iter != myset.end();++iter){cout << (*iter)->a << endl;}return 0;}

運行結果:

參考資料:

http://www.cppblog.com/cc/archive/2006/03/12/4045.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.