C + + Learning Notes "Review"

Source: Internet
Author: User
Tags processing text shallow copy

Programmer Sutra Pavilion---C + + programming Bingshu

static member class: Easy to be shared by all objects of class;


static data member static int A: Creates and initializes a-------------only once the program starts executing (contrast: non-static data members are created and initialized multiple times with the creation of the object);


static member function static void Fun (): belongs to a class, does not belong to the object, can only access static data members, there is no this pointer;

pointer to class member:

Pointer to data member:

Pointer to static data member: A *PA=A::M_PA;

Pointer to non-static data member: int a::*px =&a::m_nx;

Pointer to member function:

Pointer to static member function: A (*PA) () =&a::m_pa;
Pointer to non-static member function: Int (a::*px) = &A::m_nx;

initialization of a class:

1, typically, if the class object is initialized by the default constructor, it can generally be assumed that he has not been initialized;

2, the relatively good class initialization mechanism is the constructor function;

3. Constructors (with the same name as the class; no return value; public function), in the absence of a write constructor, the program automatically generates the parameterless constructor;

4, copy constructor: its formal parameter is the object of this class reference, the role is to use an existing object to initialize the same kind of another new object;

5, deep copy "' Aggregation Relationship '" & Shallow Copy "' Acquaintance relationship '";

destructor:

1, the system automatically calls at the end of the class object life period, returns the related resources or automatically releases the resources, solves the memory leakage problem; Contrary to the constructor,

2, delete can only empty heap space, can not clear stack space;

3. Virtual destructor: The pointer to the base class points to the object of the derived class and deletes the derived class object with a pointer to the base class;

4, when and only if the class contains at least one virtual function to declare the virtual destructor

5, base class: Abstract class preparation is used as a base class; Generally contains virtual functions, there must be a virtual destructor function, pure virtual function will produce abstract class;

6, when a class needs to be inherited, be sure to use the virtual destructor function:

----------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------

1, &: Take address operator;

*: pointer operator;

Global variables: external variable extern

2. In order to avoid the same file being include many times, there are two ways in C/s + +, one is #ifndef way, one is #pragma once way. There is not much difference between the two compilers that can support both, but there are still subtle differences.
Mode one:
#ifndef __somefile_h__
#define __somefile_h__
..//Some statements
#endif
Mode two:
#pragma once
..//Some statements
#ifndef的方式依赖于宏名字不能冲突, this will not only guarantee that the same file will not be included multiple times, but also that two files with exactly the same content will not be accidentally included. The downside, of course, is that if a different header file's macro name accidentally crashes, it may cause the header file to exist, but the compiler insists it cannot find the state of the statement-a situation that is sometimes very maddening.
#pragma once is guaranteed by the compiler: the same file will not be included multiple times. Note that the word "same file" here refers to a physical file, not two files of the same content. The advantage is that you don't have to bother to think of a macro name, of course, there will be no macro name collision caused by strange problems. The disadvantage is that if a header file has multiple copies, this method does not guarantee that they will not be included repeatedly. Of course, the repeated inclusion is more likely to be found and corrected than the "Can't find declaration" problem caused by the macro name collision.
#pragma once method is produced after #ifndef, so many people may not even have heard of it. At present, it seems #ifndef more respected. Because #ifndef is naturally supported by language and is not limited by the compiler, the #pragma once approach is not supported by older versions of the compiler, in other words, it's not good enough for compatibility. Maybe, in a few years, when the old compiler dies, it's not a problem.
I also see a usage that puts the two together:
#pragma once
#ifndef __somefile_h__
#define __somefile_h__
..//Some statements
#endif
It seems to be an advantage to have both. However, as long as the use of #ifndef there will be the danger of macro-name conflict.

3, Documents: #stdio. h

buffer files: processing text files;

Non-buffered file: processing binary files;


#<fstream>

Ifstream & Wifstream: Read files, Wifstream Word read operations;

Ofstream & Wofstream: Data written to file, Wofstream Word read file;

fstream &wfstream: Read/write file, Wfstream Word read operation;

filebuf &wfilebuf: The actual character reads and writes, wfilebuf the word read/write operation class;


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.