SCW Development experience: Why use private classes? Key points of realization of C + + private class and its experience

Source: Internet
Author: User

The main points and experiences of scw-c++ private class based on Directui

2015.03

In the process of designing the SCW . Keep abreast of new knowledge, as a C + + beginner, new knowledge is like dew. Especially in the learning process, to see other people's good experience and methods, like the discovery of the new continent. Although the SCW framework is still under construction, it is not yet directui that step, but the process of learning and developing is full of surprises.

These two days are refactoring SCW, which is already the third draft. Although my foundation is shallow, the knowledge is not deep, but I understand a truth. If you want to get a good point of the SCW Interface development tool. There is no good framework, and the subsequent realization of the so-called Directui are virtual. If you want to make an ideal frame, you must learn the basic knowledge.

So, these days, I go back to my head to look at the knowledge of C + + class structure, carefully and carefully understand the management of memory and pointers in C + +. In your online roaming, finally found some good knowledge points. In C + + class through the private class to achieve compatibility and extension of class, improve the framework of the class, packaging class of experience. is a good harvest.

The implementation of private classes is commonly used in QT. the q_d in QT, Q_Q, is the reference interface to the private class. With two days of understanding and practice, some of the private classes are now summed up.

When I first approached this concept, I also had the following questions:

1. What is the implementation of a private class?

2. how to achieve?

3. What is the need for private classes? Why reference a private class to this concept or method

4. What's the downside?

By learning, coupled with hands-on practice, the above questions were resolved or understood by me one by one. Also, it is decided to use this good method when doing Directui's SCW interface framework.

1. First, what is the implementation of the private class

In a nutshell, it is in a class ClassA thatmost of the functionality is achieved through the private classb of another class. and ClassB only for the ClassA service, that is, other classes, is not directly accessible to ClassB, this is the private class of ClassA.

At first glance, it seems unnecessary, because a class is designed to achieve a task or purpose. Just use this class to do it right. Why do you have to do it with another class? This leads to a second question, what are the benefits of using private classes?

We need to start talking about the implementation of private classes to better understand the benefits of this implementation:

ClassA refers to private class ClassB, not simply by ClassB to implement its function. If that's the case, it's not necessary. Is it better to use ClassA directly ? The event is not so simple. ClassA 's use of private classes has structural advantages and needs. Let's take a look at his implementation:

Summarize the implementation focus of the private class:

  1. Declare the main class body structure in the main class. h header file. Also declare a friend of the private class that the main class needs to refer to (that is, friend Class ClassB) and declare a member variable ClassB b or interface that accesses the private class;

  2. 2.    No need to be in the main class. h contains the private class header file. Also do not work in any one. h contains the private class. h This is the point.

  3. The main class is included in the implementation source file (. cpp) of the main class . h header files and private class. h header files. It also implements the function of the main class and the private class in the source file. (You can also use several. cpp separately to implement the main class and private classes, where the code is written without any effect.) )

For example, the following:

File Class_a.h

Class ClassA {protected:    friend class ClassB;//Declare friend Relationship public:    ClassB  B;//private class Access entry    void func () ...}

Then declare ClassB in another header file Class_b.h . The structure body of the class. (Do not write it specifically)

The focus is on implementing the ClassA code file classA.cpp . It also contains Class_a.h, class_b.h.

In this way, the ClassB class and the classB.h file are private, and theclass_b.h file is referenced only in the ClassA.cpp file. There is no need to refer to the Class_b.h header file anywhere else in the project .

At this point, it can be imagined that arbitrarily modifying the structure of the ClassB class, i.e. modifying the Class_b.h header file, will not affect other modules in the project. It doesn't even affect class_a.h files.

As to what functions put in ClassA to achieve, which functions to ClassB to achieve, this is their own personal design ideas and arrangements. You do not even need to ClassA to implement any specific content, just as an interface class is also OK.

Let me give you a small example:

ClassA want to implement X, Y value reading and writing. This is the only way ClassA can be implemented.

Class classa{    int X (int value = 0, Boo Iswirte = False) {      if (iswrite)//write flag        b->x = value;//assuming B is clas Private class object for SA        return b->x;    };    

A simplifies the declaration of member variable x, and a function solves read and write. Of course, this is just a simple example, and more implementations require personal digging.

If you see this, you can think of something that shows you are starting to understand the need for a private class.

Why use such a structure? This is the core of the existence of private classes. It is also the benefit and necessity of private class.

2. Benefits of using private classes.

Through the simple instructions above, we can initially find several benefits of private classes.

    1. You can simplify the structure of the main class.

    2. You can encapsulate the functionality of the main class.

    3. Extensions to the main class feature are implemented in private classes without affecting the header file structure of the main class. such as declaring intermediate variables, adding some intermediate processing functions, and so on.

    4. greatly improves the compatibility and extensibility of the main class.

    5. Can achieve the purpose of hidden design ideas (this is for reference only)

Use these small examples to illustrate:

class ClassA, We need to add another feature in the set X and <n The structure of adding a constant N is the base class of deep bottom, and your project is very large, many documents, can imagine.

This will not happen if we are only modifying the ClassB header file, and the. CPP code file. Although all are the same purpose, the same changes, but the results are very different.

The reality will be more complicated. We just use a simple example to extend this. In practice, we may need to add new variables and handler functions for the completion of some new tasks. However, if these additions are done in ClassB , the ClassA structure does not need to be moved at all. This guarantees the compatibility of the ClassA , and also achieves the purpose of extending the ClassA function. It also reduces the reliance on the enemy files.

Specific application scenarios to imagine yourself. If ClassA needs to constantly add new features in the update, new variables. You don't even need to change any of the classA structures, just call ClassA 's private members directly (and of course, this is not recommended). For example A.privateobject ()->newfunc (). or A.privateobject ()->width = 100; that kind of implementation is possible.

If ClassA is a public reference class in the externally published DLL file, ClassA Each modification of the structure will have the possibility of incompatibility with the update of the DLL version. The private class of design science can achieve the compatibility of binary code of the optimizer or library file.

3. Disadvantages.

Compared with the advantages, the use of private classes, it can be said that the disadvantage is not a thing.

    1. Increased code volume. Because the function implements the transfer, it is necessary to add some transcoding code in the main class, and also increase the workload.

    2. The code is no longer clear. Previously directly processed functions are no longer straightforward. For later developers, it takes more time to get acquainted.

    3. Especially when fixing bugs , because the process is no longer straightforward, you need to re-shape the design ideas.

    4. If the design is inappropriate, private classes can cause trouble. Make the program structure seem confusing.

4. key points:

Use the class's private class implementation method rationally. Can achieve the above mentioned benefits.

Private class design only for important classes . Do not implement private classes in any class.

A set of unified implementation methods, such as the design of some convenient macros, standardize the standard of the transfer interface.

To better understand private classes, it is recommended to go deep into the implementation of classes in QT. and self-designed cases to complete some tests.

I used to think that classes are already a good package and compatible when I first learned the concepts of classes.

However, after we have designed a class, we will often improve a class because of the accumulation of knowledge or the increase of demand, or because of the richness of the customer's needs or functions. In the absence of the concept of private classes, we typically add new member variables or member functions directly to the class's original structure to complete these modifications. If you refer to the concept of a private class, the original structure of the main class does not require too much modification or even modification. Class encapsulation and compatibility has been further sublimation!

I have adopted a private class in my SCW interface library. The application of private class is also tested and perfected.

For SCW's personal ideas, see: Personal vision and goals for SC design planning based on Directui

SCW Directui Interface Design Communication, please add QQ Group: 177312461 or personal qq:48018276

SCW Development experience: Why use private classes? Key points of realization of C + + private class and its experience

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.