C ++ bit drip

xiaoxiao2021-03-06  62

Objects as a parameter transmission function, and the problem that the function returns an object

1. When the object is directly transmitted to a function as a parameter (PASSING Object By Value), a new object is generated as a copy of the object.

But you need to pay attention to:

When the new object is created automatically,

Constructor is not called, but

Copy constructor is called; because the constructor is an object that is initialized, as a parameter of the incoming function, it is a copy of the existing object.

When the function ends, the life cycle of the newly created object will end for parameters, so this time, object

The destruction function (Destructor) will be called.

2. When the function returns an object, it is necessary to pay attention to it.

If the return value is not explicitly assigned to an object, a temporary object is automatically created for other possible calculations; the same, object returns

(Whether it is temporary object), object

The copy constructor is called instead of constructor; when the temporary object completes the mission (return to HAS BEEN RETURNED), the object

The destruction function (Destructor) will be called.

Therefore, the object is used as a parameter transmission function, and when the function returns an object, it is necessary to pay attention to the case of the pointer in the member of the object.

About Copy Constructor

When a variable is created by an existing object, the replication constructor is called.

Existing in three situations (note, when assigning operation, it will not be called):

1. When a variable is declared, the variable is initialized

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

Person Q ("Mickey"); //

Constructor is called

Person R (p); //

Copy constructor is called

Person P = Q; //

Copy constructor is called

P = q; // This is an assignment operation,

Constructor and

Copy constructor

Not called

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

2, when the object is directly transmitted to the function as the parameter (PASSING Object By Value)

3, when the function returns an object

reference:

Http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html

● About

Operator overloading

Operator overload can pass

Member function (MEMBER FUNCTION) and

Friendship function (Friend Functions).

But there is a limit:

1, can not change the priority of the operator

2, can't create new operators

3, can not be overloaded :: SizeOf?:. ** (Thinking In C :. * **)

4, heavy load does not mean that it is heavy as a heavy load =, other - =, etc.

5, overload =, [], ->

, When the operator, only the member function can be achieved.

Thus, forced the left operand type is the defined class.

6, and - overload require special processing (the difference between A / A- and A / - A)

7, = overload requires special consideration (self judgment, release and distribution of pointers)

THINKING IN C : Murray has the following suggestions for operator overloading.

Operator recommends all one yuan symbol member function = () [] -> -> * must be a member function = - = / = * = ^ = & = | =% = >> = << = member functions all Non-member function ● About function hidden subclasses can hide the function of the parent class by changing the return value or parameter, but if the function is a virtual function, it can only be hidden by changing the return value by changing the return value by changing the return value. Because the virtual function of the same parameter can only be overwritten without hiding, the overlay can only return the same return value type (or the type of derived class). ● About inheritance 1, constructing function, replication constructor, destructuring function, assignment (=) operation symbol function does not automatically inherit, but synthesized 2, if the subclock is clearly defined, replication constructor / assignment The operation symbol function must be clearly called the corresponding replication constructor / assignment operation symbol function of the parent class, otherwise the default constructor of the parent class will be called. 3, assignment operation symbol function is only integrated with the same type of object. From << Thinking In C >> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- -

//:!: Copyright.txt

(c) 1995-2004 MindView, Inc. All Rights Reserved.

//: C14: SynthesizedFunctions.cpp

// Functions That Are Synthesized by the Compiler

#include

Using namespace std;

Class gameboard {

PUBLIC:

Gameboard () {cout << "Gameboard () / n";}

Gameboard (const gameboard&) {

COUT << "Gameboard (const gameboard &) / n";

}

Gameboard & Operator = (const gameboard&) {

Cout << "Gameboard :: Operator = () / N";

Return * this;

}

~ Gameboard () {cout << "~ Gameboard () / n";}

}

Class game {

Gameboard GB; // Composition

PUBLIC:

// Default Gameboard Constructor Called: Game () {cout << "GAME () / n";

// You Must Explicitly Call The Gameboard

// Copy-Constructor or the default constructor

// is Automatically Called Instead:

Game (Const Game & G): GB (g.GB) {

COUT << "Game (const game &) / n";

}

Game (int) {cout << "Game (int) / n";}

Game & Operator = (const game & g) {

// You Must Explicitly Call The Gameboard

// Assignment Operator or no assocignment at

// all happens for GB!

GB = g.GB;

COUT << "Game :: Operator = () / n";

Return * this;

}

Class Other {}; // Nested Class

// Automatic Type Conversion:

Operator other () const {

Cout << "Game :: Operator Other () / N";

Return Other ();

}

~ Game () {cout << "~ Game () / n";}

}

Class Chess: Public Game {};

Void f (Game :: Other) {}

Class checkers: public game {

PUBLIC:

// Default base-class constructor called:

Checkers () {cout << "Checkers () / n";}

// You Must Explicitly Call The Base-Class

// Copy Constructionor or the default constructor

// Will Be Automatic or Called Instead:

Checkers (Const Checkers & C): Game (c) {

COUT << "CHECKERS (Const Checkers & C) / N";

}

Checkers & Operator = (Const Checkers & C) {

// You Must Explicitly Call The Base-Class

// Version of operator = () or no base-class

// Assignment Will Happen:

Game :: Operator = (c);

COUT << "Checkers :: Operator = () / n";

Return * this;

}

}

Int main () {

Chess D1; ​​// default constructor

Chess D2 (D1); // Copy-Constructionor

//! Chess D3 (1); // error: no int cabletructor

D1 = D2; // Operator = synthesized

f (d1); // type-converness is inherited

Game :: other go;

//! D1 = Go; // Operator = not Synthesized

// for Differge Types

CHECKERS C1, C2 (C1);

C1 = C2;

} ///: ~

-------------------------------------------------- ------------ Gameboard () Game () Gameboard (Const Game &) Gameboard :: Operator = () Game :: operator = () game :: operator = () gameboard :: operator = () gameboard :: operator = () Gameboard (Const Game &) Game (Const Game & C) Gameboard :: Operator = () Game :: Operator = () Checkers :: Operator = () ~ Game () ~ Gameboard () ~ Game () ~ Gameboard () ~ Game () ~ Gameboard () ~ Game () ~ Gameboard () ----------------------- --------------------------------------- ● Abnormal 1, when an exception is all When the level is not captured, terminate () will be called 2. When an exception is thrown in a sectic function function, TERMINATE () will be called; when a global variable or STATIC variable constructor is analyzed When the constructor throws an abnormality, Terminate () will be called. 3. When an object throws an abnormality in the constructor, its destructor will not be called.

Reference: http://www.fredosaurus.com/notes-cpp/ << Thinking In C >> 2ed 2000 by Brouce Eckel Jady Leung 2004 Created on February 28, 2005 Updated, Add << Thinking IN C >> Content

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

New Post(0)