C + + header files and implementation of the document detailed _c language

Source: Internet
Author: User
Tags class definition function definition

In the C + + programming process, as the project becomes larger, the code will become more and more, and difficult to manage and analyze. As a result, the header (. h) file and the implementation (. cpp) file are separated in C + +, and the concept of package is also available.

To start with C, C # as a "mother tongue" I just started to follow the tutor to learn C + + for this aspect still feel very vague. Although I can with C's knowledge to face C + + syntax specification, use C # thought to comprehend the use of C + + class. But C # definition and implementation is in a file (in fact, in the class), and the use of C is just the beginning of programming, the written program as long as a file is enough. Therefore, there is always a tangle of package understanding of C + + and. h files and. cpp files.

Fortunately, the instructor has a detailed ppt to let me know, once for the understanding of package more clearly. In simple terms, a package is made up of. h and. cpp files with the same name. Of course you can. Any one of these files: only the package of the. h file can be the definition of an interface or template (template), and only the package of a. cpp file can be an entry for a program.

Of course more specific detailed explanation, welcome to download Tutor's teaching ppt-package to learn more.

But here's what I want to talk about. h files and. cpp files

Knowing that package is only a relatively macroscopic understanding: we extend and revise our programs in the project with package as the editing object. When you write code that is specific to what should be placed in the. h file, and what to put in the. cpp file, I am confused again.

Although Google has given me a lot of links, most of the explanations are too general: The statement is written in the. h file, and the definition implementation is written in the. cpp file. There's no mistake in that explanation, but when you do, you'll find that you don't know where to hit the code.

So I threw the question to the mentor, who patiently gave me a detail of how to do code separation in C + +. Unfortunately, the first time I listened to, but did not listen to understand, and the original knowledge of C + + is not deep, so there is no deep impression.

After several trials and experiences of the project, I asked the tutor again, and he patiently explained it to me again (I swear he never forgot that I had asked the same question), and this time I recorded it.

In order not to forget, I summed them up here.

Overview

Non-template type (none-template) Template type (template)
Header file (. h)
  • global variable declaration (with extern qualifier)
  • Declaration of global functions
  • Definition of global function with inline qualifier
  • declarations and definitions of global template functions with inline qualifiers
  • Definition of Class
  • Declaration of class function members and data members (inside a class)
  • function definition within class definition (equivalent to inline)
  • Initialization of a data member with a static const qualifier inside a class
  • function definition outside of class definition with inline qualifier
  • Definition of template class
  • Declaration and definition of a template class member (definition can be placed within a class or outside of a class, without the need to write inline)
Implementation file (. cpp)
  • Definition (and initialization of global variables)
  • Definition of global function
No

* Affirm: declaration
* Definitions: definition

Header file

All the contents of the header file must be contained in the

#ifndef {Filename}
#define {Filename}

{Content of head file}

#endif
In order to ensure that the header file is referenced by multiple other files (include), the internal data is not defined multiple times to cause errors

Inline qualifier

In the header file, you can tell the compiler with the inline qualifier on the function, which is very simple and can be embedded directly into the call definition.

Of course inline functions are not necessarily implemented by compilers as inline, and if the function is too complex, the compiler rejects inline.

So simply put, the code is best short to only 3-5 lines as inline. There are loops, branches, and recursive functions that are not used as inline.

For functions that define implementations within a class definition, the compiler automatically treats as a inline request (and is not necessarily inline). So below, I refer to the function member with the inline qualifier and the function member written in the class definition body collectively as "the function member to inline"

Non-template type

Global type

As stated in the preceding general statement: The statement is written in the. h file.

For a function, a function without an implementation body is tantamount to a declaration, and for a data type, including a basic type and a custom type, the declaration needs to be decorated with extern.

These global functions and global variables are then defined, implemented, or initialized in the. cpp file.

However, the instructor has repeatedly stressed: do not use global functions and global variables. After the use of the consequences of the present is to hand over the work item will be deducted points. Of course, there are no excuses and solutions that are not available, but they are beyond the scope of the current discussion.

Custom type

For custom types, including classes (class) and structs (struct), their definitions are placed in the. h file. The statement and definition of its members is more complicated, but look at the table above, it is quite clear.

Function members

Function members regardless of whether they have a static qualifier, their declarations are placed inside the class definition of the. h file.

The definition of the function member to be inline is placed in the. h file, and the implementation of other functions is placed in the. cpp file.

Data members

The declarations and definitions of the data members are placed inside the class definition of the. h file. For data types, the key question is where to place the initialization.

For data members that contain only the static qualifier, its initialization is placed in the. cpp file. Because it is common to all class objects, it must be initialized appropriately.

For data members that contain only the Const qualifier, its initialization can only be done in the initialization list of the constructor. Because it is not assignable once initialized, it must also be initialized appropriately.

For data members that contain both a static qualifier and a const qualifier, its initialization and definition are simultaneous. It also has to be properly initialized.

For a data member that has neither a static qualifier nor a const qualifier, its value can be modified only for this object, so we do not care when its initialization is performed.

Template type

In C + +, the template is a development tool, it is similar to the generic type of C#,java, but not the same. In the past, I have always felt like generics, templates this kind of things I may not need to use all my life. But in the teacher's forced use, I really understand the strong template, but also really know how to use the template, further is how to design templates. But this is not words can finish, don't say more.

The most important point for a template is that when it is defined, the compiler does not compile it because none of its entities are available.

Only after the template is materialized (specialization) (used on a specific type) does the compiler compile the template based on the specific type.

So when you define a template, you will find that the compiler basically does not report an error (I was also very happy: I write code will be no error, coherent), also do not have smart tips. But when it is specifically used in a class, errors will appear in large tracts, but often can not be accurately positioned.

So the design template has a set of ideas and ways to design the template, but this is also biased with the subject of this article.

Because of this specificity of the template, it does not have its own exact definition, so we can't put it in the. cpp file, and write all of them in the. h file. This is also to allow the compiler to find out where all the templates are defined in order to actually define the method when the template is materialized.

As for the definition of the template class function member, the mentor's opinion is placed outside the class definition, as you can see the methods and data at a glance when you look at the class, and I see the implementation of its standard library when I use Visual Studio, all within the class.

Maybe I'm used to the C # style, I prefer to write them all within the class, and because in the development process, the editor used has a powerful function: code folding.

Of course, there are other reasons to write outside the class, for each function member of the implementation of the template type needs to be written as a qualifier, the class name qualifier to write again.

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.