Use using to simplify restricted proxy code and adjust access

xiaoxiao2021-03-06  51

Let's start from the code, simply, code omit the STD namespace: class myobject {object_id obj_id_; class_id class_id_; map

Properties_;

Static const type_dict & getdict ();

...

}

This is a code that is organized from the actual project. It is a simple explanation: Properties stores the property of Object, and myObject rely on a type system support. This type of system model is Type_Dict. Class_ID_ is the type of object to which the object instance is recorded. Obj_id_ is an ID of the object instance. All attributes of this object can be accessed from the outside, but I can't open Properties_, because this way, external You can just specify a Property_ID inserting the map, and this property_id may not contain the type of Class_ID_, or, it is illegal, which will cause inconsistent in MyObject. So, in addition to completing the implementation of Operator [] and Clear, I should do this: public: const_iterator begin () const {return property_.begin ();} iterator begin () {Return Properties_.begin ();}. .. Their implementation is very simple, just a line: calling Properties's corresponding way, but I have to recover again, this is a copy & paste work, always let me want to doze off, the result, I am always wrong, Because I always forget to do some necessary modifications. If there is a syntax like this: public: proxy const_iterator property_.begin () const; or: proxy proties_.nd; I think I will be more happy. In addition to less code, the key is that the code becomes clear, the programmer is usually lazy, and I am too lazy and forgetful. Keep the code clearly. So, the code becomes this: Class MyObject: Private Map

{

Typedef Map

CONTTYPE;

Object_id obj_id_;

Class_id class_id_;

// map

Properties_;

Static const type_dict & getdict ();

...

}

Then, I can write this: public: const_iterator begin () const {return contType :: begin ();} iterator begin ();} This seems to be at least clear, but still can't Let me be satisfied, I still make a mistake: I put two C & V, then change BeGin into END, and the result will be leaked by: (Finally, using has reached my purpose, I decided to write: public: Using contTy :: end; using lastpe :: insert; this does not cause access to the issue of access, see, this is the proxy property_.nd of I originally envissed; how similar way. Don't just It is simple, most important is the semantics of expression: the same method of transferring container. I think any programmer will first believe in STL method, not what I wrote, even if my code is only one line, it may be wrong .using method if If you make an error, you will not pass it when you compile. If you think, using just introduces a name, then using should also be effective. Let's try this: using lastpe :: iterator; very good, it works, And there is no need to add a TypenAme in front of the contType :: Iterator in front of Typedef. So I decided to introduce the type of the type in the Map into myObject in MyObject, there are more than a dozen lines of code. Flora, and using a member function Same, the compiler will immediately report the error immediately. I seem to solve this problem very well, just a little bit of trouble: I got a lot of MAP methods, introduced MAP Typedef. Actually only Map SWAP, Operator [], etc., a few methods need to be modified, but I have to write a dozens of lines of code for other parts. Go back to my purpose: I just prevent some external to myObject to modify, avoid leading The inside is inconsistent. As a result, I have to put a lot of Typedef and const methods. Increased code length is second, it disturbed my sight. Intuitive, others will think that I am because of these methods. So I apologically, but the true meaning is: I don't care about them (like count this kind of thing, I really don't know if I need to expose it), so let them keep it. This intuitive metaphor and true intention The contradiction between, let me worry that one day he will defind someone. There is better Is the way? Let us see such a code: struct a {void fun () {}}; Class B: public a {using a :: fun;}; try this sentence: b (). Fun (); compilation error! The report cannot access private members. This means that we can change access to some base class members! In fact, this kind of access limit can be relaxed or strengthened, which is great! So my MyObject can be turned this: Class MyObject: Public Map

{

Typedef Map

CONTTYPE;

Using contType :: operator []; // Here, access to the Clear and Operator []

Using contType :: clear;

PUBLIC: VOID CLEAR ();

...

}

转载请注明原文地址:https://www.9cbs.com/read-117644.html

New Post(0)