One of C ++ Shen Shuographies (Class Designers)

xiaoxiao2021-03-06  14

Does your class need a constructor?

Is your member variable private?

If the class designer uses a member variable, the designer must guarantee that the member variable can correctly reflect the actual number of values ​​at any time, because there is no way to know when the user will come to access this information.

Does your class need a non-arranging constructor?

If you want to declare that objects that can be explicitly initialized, you must explicitly write a non-arranging constructor.

Is every constructor initialized all data members?

In general, each constructor must have a clearly defined value for all data member settings. If you don't do this, it is likely to cause errors. Of course, it is not necessarily the case, and some data will only make sense after their objects have a certain period of time.

Does your class need a destructor?

If the class is assigned a resource, these resources will not be automatically released by the member function. The destructor is needed.

Does your class need a false abstract function?

Declare an object of a derived class automatically declares an object of a base class. The object of derived class can also be considered to be the object of its base class, but it is otherwise. Therefore, C allows a pointer to the base class object to point to the object of its derived class (derived class contains a member function of the base class), but does not allow a pointer to a derived class object to point to the object of its base class (base class does not necessarily contain derived class Member function).

Therefore, when the base class and the derived class have the same member function, the base class pointer is a member function that is the base class that is the base class, whether pointing to the base class object or pointing to a class object. (Of course, the derived pointer points to the derived object, calling the members of the derived class)

There is an example below, explaining this problem:

#include

Class a {

PUBLIC:

// The virtual function is first defined in the base class for the first time, the keyword Virtual cannot be omitted.

Virtual void show () {cout << "A :: show / n"};

}

Class B: Public a {

PUBLIC:

Void show () {cout << "b :: show / n"};

}

Void main () {

A a, * pa;

B b;

PA = & A; PA-> show ();

PA = & B; PA-> how ();

}

The result of the program is:

A: Show

B :: show

If the member function show () of class A is not a virtual function, the result of the program run should be:

A: Show

A: Show

Another program is illegal:

A a a;

B B, * PB;

PB = (b *) & a;

PB-> show ();

Such a program may be able to run (because I have not tried), but this is a very bad habit. If there is no member function show () in the base class A, an abnormal situation will occur after the program is running. This is because the program enforces the base class pointer & a to derived class pointer PB, and then the derived class member show () that does not exist in the PB reference object A.

Does your class need to copy constructor?

The key is whether or not the object of the class is equivalent to copying its data members and base classes. If it is not quite, you need to copy the constructor. Such as:

INT x = 0, y = 1;

X = Y;

Similar to the situation of replica objects

If you don't want the user to copy the objects of the class, declare the copy constructor (possibly the assignment operator) is private:

Class think {

PUBLIC:

// ...

Private:

Thing (Const Thing &);

Thing & Operator = {Const Thing &};

Does your class need an assignment operator?

If you need to copy constructor, you will need an assignment operator. If you don't want the user to set the object in the class, you will be prioritized. The assignment of class X is defined by x :: Operator =. Usually, Operator = should return a X & you, and

RETURN * THIS;

End to ensure consistent with the built-in replication operator.

Does your ingredients do the object to the object itself correctly?

It is possible to destroy the original object before you have not implemented replication. E.g:

Class string {

PUBLIC:

String & Operator = {const string & s};

Private:

CHAR * DATA;

}

// very obvious but incorrect implementation

String & string :: Operator = {const string & s}

{

DELETE [] DATA;

Data = new char [strlen {s.data} 1];

STRCPY {data, s.data};

RETURN * THIS;

}

// Correct implementation method 1

String & string :: Operator = {const string & s}

{

IF (& S! = this) {

DELETE [] DATA;

Data = new char [strlen {s.data} 1];

STRCPY {data, s.data};

}

RETURN * THIS;

}

// Correct implementation method 2

String & string :: Operator = {const string & s}

{

Char * newdata = new char [strlen (s.data 1)];

Strcpy {newData, s.data};

DELETE [] DATA;

Data = newdata;

RETURN * THIS;

}

Does your class need to define a relational operator?

If your class logical supports equal operation, then provide Operator == and Operator! = May be very beneficial. Similarly, if the value of your class has some sorting relationship, it may want to provide the remaining relationship operator. These relationship operators may also be needed even if the user does not want the user to use the relationship. As long as they want to create an ordered collection of your type, you must provide relationship operators.

When you delete an array, do you remember to use DELETE []?

Remember to add Const in the parameter type of the replication constructor and assignment operator?

The replication constructor should be x :: x (const x ". In fact, since the binding a non-Const reference is illegal, use x :: x (x &) as a replication constructor that does not allow copying of any special expressions. The same reason is also applicable to assignment: using x :: operator = (const x ", not x :: operator = (x").

If the function has a reference parameter, should they be a const reference?

Only when the function wants to change the parameters, it should have reference parameters that do not have a Const declared.

Remember to properly declare that the member function is constant?

If you are sure that a member function does not have to modify its object, you can declare it for const, which uses it for Const objects. E.g:

Template Class Vactor {

PUBLIC:

INT length () const; // Get length, intlength (); is an error INT length (int); // set the length, return the previous one

// ...

}

Otherwise, we will encounter the following questions:

Template

INT Padded_length (const vector & v, int N) {

INT k = v.length (); // OOPS!

RETURN K> N? K: N;

}

The OOPS! Of the OOPS! Unless there is a consecutive constity, the row marked with the OOPS!

March 13, 2005

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

New Post(0)