C + + Learning notes (5)

Source: Internet
Author: User

1. Two adjacent string literals separated by spaces, tabs, or newline characters, can be concatenated into a new string
cout<< "a multi-line"
"String literal"
"Using Concat"
<<std::endl;

2. If the declaration has an initialization formula, it can be considered as defined, even if the declaration is marked as extern
extern double Pi=3.14//definition
An extern declaration can contain an initializer only if it is outside the function

3.const must be initialized at the time of definition, which is not the same as Java, and the final of Java can be initialized in the constructor.
Private
const int _conv=2;
The above code error indicates that only static constant integer data members can be initialized in the class
That is, class members only const static can be initialized in the definition!!!

4. When defining a non-const variable in the global scope, it can be used throughout the program.
When a const variable is defined in the global scope, it is only available in the defined file and cannot be used by other files.
You can access the const object throughout your program by specifying that the const variable is extern.
When declaring an externally defined Const object, you must also bring the Const keyword in the declaration, otherwise an error is made.
Extert const int Counter=100;//defines in file_1.cc
Extert const int Counter;//declare in file_2.cc
for (int i=0;i<counter;i++) ...

5. When defining a global variable, the definition code needs to be written in A. CC (. cpp) file and cannot be written in the. h file
While extern a variable, extern can be placed in the. h file or in a. CC (. cpp) file. can also be placed inside the method.
When you define a const global variable, you can put it in an. h file.
When you want to use a global variable, you just need to declare it, you don't need to define which file the global variable is defined in, and you don't need to include the class that contains the global variable. Just make sure the global variables are defined as well.

6. A reference is another name of the object.
A reference is a composite type that refers to a type defined with another type.
A reference must be initialized with an object of the same type as the reference.
Initialization is the only way to indicate which object the reference points to.
A const reference is a reference to a const object
A non-const reference can only be bound to an object of the same type as the reference.
A const reference can be bound to an object of a different but related type, or to a right-hand value.
Const references can be bound to non-const objects

int &refval=10;//error
const INT &refval=42;//ok

int val1=10;
int &refVal1_1=val1;
int &refval1_2=refval1_1+1;//error

const int val2=10;
const INT &refVal2_1=val2;
const INT &refval2_2=refval2_1+1;//ok
int &refVal2_3=val2; Error

int val4=1.11;
const INT &refVal4_1=val4;

Double val3=10;
const int &refval3_1=val3;//binds an int refer to a double value
The above sentence is equivalent to the following two lines of code in the compiler:
int temp=val3;
const INT &refVal3_1=temp;

7. The class definition of the header file is a semicolon-

8. The initialization of a class member cannot normally be used as part of its definition,
When you define a data member, you can specify only the name and type of the data member.
A class does not initialize a data member when it is defined in a class definition, but also controls initialization by a special member function of the constructor.

The only difference between the 9.struct and class keyword definition classes is the default access level. By default, the member of the struct is a member of Public,class private

10. Header files are used for declarations rather than for definitions, because header files are contained in multiple source files, and all should not contain definitions of variables or functions!!! Understand well.
There are three exceptions to this rule where header files should not contain definitions, and header files can define classes, const objects and inline functions that are already known at compile time.
These entities are defined in the header file because the compiler needs their definitions (not just declarations) to produce the code.

11.c++ supports the so-called separate compilation, which is very different from Java, where a class is defined in Java, meaning the implementation of a class. But in C + +, a class is divided into two parts, an interface, and an implementation.
When defining a, is the interface that defines the class, and this interface is defined in the header file. The implementation of a class can be implemented in any file.

12. Once we have defined the const variable in the header file, each source file containing the header file has its own const variable. Its name and value are the same.

13. If a const variable is defined in a header file a, the header file B has a, and the header file C include a and b compile an error, prompting "find one or more multi-defined symbols"

14. Some const objects are defined in the header file???? Don't understand!!!

C + + Learning notes (5)

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.