05-04-25

xiaoxiao2021-03-05  32

* Member initialization table format:

Constructor name (parameter table): a member name (initial value) in the class, a member name (initial value), ... {function body};

※ Prevent this parameter from misurbing parameter variables:

Return value type function name (const parameter, const parameter, ...);

※ Prevent all variables in the object of the object of this function:

Return value type function name (parameter table) Const;

※ Prevent other functions from writing to return the address or reference:

Const Return Value Type Function Name (Parameter Table);

* Copy constructor (automatically calls when using an object to initialize another object)

Features: With the same name, do not specify back; only 1 parameter is a reference to the class object.

Format: :: ( & ) {}

Class demo {

PUBLIC:

Demo (const char * s) {...}; // constructor

Demo (Const Demo & St) {...}; // Copy constructor

Demo () {...}; // does not automate

~ Demo () {...} // destructor

}

...

Demo H ("first");

Demo r = h; // copy all members of the object H to the object R

/ / If you write a Demo R; r = h; no copy constructor is called

※ If you don't want to write a copy constructor and assignment function, it is not allowed to use the default function generated by the compiler. You can declare the copy constructor and assignment function as a private function, and you can write code. Such as:

Class A

{...

Private:

A (Const A & a); // Private copy constructor

A & OPERATE = (const A & a); // Private assignment function

}

If you write as follows:

A b (a); // Call private copy constructor

B = a; // Call the private assignment function

The compiler will point out errors because the outside world cannot operate A private function.

* Friends function

Putting with Friend before the member function name, it is a non-member function that can access the private member, which is the same as the normal function in definition and call.

转载请注明原文地址:https://www.9cbs.com/read-31843.html

New Post(0)