[C ++] problems caused by pointer constants and constant pointers

Source: Internet
Author: User

Today I am writing a C ++ProgramYou need to use the STL set container, and I want it to use custom sorting rules for automatic sorting. In addition, the element type in set is a pointer to a custom struct. Errors always occur when running the results.

The following first posts the originalCode:

 
# 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 ;}

An error occurred while compiling and running the result in vs2008. The error message is as follows:

Obviously, the problem lies in the sorting criterion of the user-defined imitation function. So what is the problem? The error message shows that the qualifier (const) is lost. However, I have a const parameter here. I am confused!

Again, let's take a closer look at the output error message. We cannot convert a parameter from type 1 to type 2. What are the differences between the two types? The answer is:The position of const is different: the difference between a pointer constant and a constant pointer! If a const is placed in a pointer, different results may occur. In the previous type, const is used to modify the pointer itself, that is, the pointer constant. After the object to which it points is determined, it cannot be modified so that it points to another object. In the second type, const is used to modify the type pointed to by the pointer, that is, the constant pointer. the pointer itself is not const and can be modified to point to another object. However, the content at the address pointed to by the pointer cannot be modified. It is const.

Next, you can modify the code. To avoid the above problems, we use typedef to redefine the pointer type pointing to the custom struct:

In this way, the original problem will not occur. The correct code is as follows:

# 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 ;}

Running result:

References:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.