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 design SCW in the process. 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 This is the third draft. Although my foundation is shallow, the knowledge is not deep, but I understand a truth. If you want to get a better SCW directui

So, these days, I go back to my head and look at C + + knowledge of class structure, careful and careful understanding C + + management of memory and pointers. 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 the private class, in QT is commonly used. 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 short, it is in a class in ClassA, Most 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. in main class . H declares a member variable that accesses a private class ClassB b

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

  3. implementing the source file in the main class (. cpp) contains 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, andtheclass_b.h file is only classA.cpp is referenced in the 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 C    Lassa private Class object return b->x;    }; Y implementation ibid.


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 setting X >0 classa add a constant in the structure n Classa is the base class of deep bottom, and your project is very big, the document is many, 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 inthe 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 in repairing BUG , 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 deeper 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've been in my private classes are used in the SCW interface library. The application of private class is also tested and perfected.


for SCW's personal ideas, see: The personal conception and goal of 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

Related Article

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.