Check table of C ++ class designers

Source: Internet
Author: User

1. Do classes need Constructors?

2. Do data need to be private?

Solution 1
Public:
Int length;

Solution 2
Public:
Const Int & length; // In the constructor, length = true_length; because it is a const variable, it can only be read and cannot be modified.
PRIVATE:
Int true_length;

Solution 3
Public:
Int length () const ;//

3. Do I need a constructor without parameters?

If there is no constructor without parameters
Point P;
Point pa [100];
Such definitions are all incorrect.

4. Is it true that each constructor initializes all data members?

The purpose of the constructor is to set an object with a clearly defined state, which is reflected by the data members of the object.
Therefore, each constructor is responsible for setting clearly defined values for all data members. (Special exceptions)

5. Do I need destructor?
If the memory is new in the class, but it has never been released, you need to release it in the destructor.

6. Do I need a virtual destructor?

If a base class B is derived from a subclass D, whether or not B has a virtual function
B * D = new D ();
Delete D; // virtual destructor is required for all B; otherwise, the wrong destructor will be called.

7. Do I need to copy the constructor?

Because the data memory is dynamically allocated. So the Destructor is required to release the data space.
Class string {
Public:
String ();
String (const char * s );
Private
Char * Data
}
Because the data memory is dynamically allocated. Therefore, destructor are required to release data space.
Also, you need to copy the constructor. If no constructor exists
String B =
Only the data address is copied. B. Data and A. Data are in the same memory. When two objects are destroyed, the memory is released twice.
If you do not want the user to copy objects of the class, declare the replication Constructor (and possibly assign value operators) as private
Class thing {
Private
Thing (const thing &);
Thing & operator = (const thing &);
}

8. Do I need a value assignment operator?

The reason is the same as that of the copy constructor.
Thing & operator = (const thing &);
End with return * this; to ensure consistency with the built-in copy operator.
When writing this function, pay attention to the assignment to yourself.
String & string: Operator = (const string & S)
{
Delete [] data; // if the value is assigned to itself, S. Data is also deleted, and all subsequent values are incorrect.
Data = new char [strlen (S. Data) + 1];
Strcpy (data, S. data );
Return * this;
}
So it is best:
String & string: Operator = (const string & S)
{
If (& S! = This ){
Delete [] data;
Data = new char [strlen (S. Data) + 1];
Strcpy (data, S. data );
Return * this;
}
}
Or:
String & string: Operator = (const string & S)
{
Char * newdata = new char [strlen (S. Data) + 1];
Strcpy (newdata, S. data );
Delete [] data;
Data = newdata;
Return * this;
}

9. Do I need to define Relational operators?

If the class logic supports equal operations, provide operate =, operate! =, It may be advantageous
If the class value has a sorting relationship, consider other operators such as <>

10. Is Delete [] used to delete data?

Using Delete [] to delete an array is a good habit. This method is used to be compatible with C. When implemented, there will be a place to record the length of the array.

11. Do you remember to add const to the parameter types of the copy constructor and the value assignment operator?

X: X (const X &)
X: Operator = (const X &)

12. If a function has referenced parameters, should they be const references?

Complex operator + (const complex & X, const complex & Y );

13. Do you remember to declare the member function as const properly?

If a member function does not modify its object (usually the get method), you can declare it as const so that it can be applied to the const object.
For example:
Int padded_length (const vector & V, int N)
{
Int K = V. Length (); // Since V is const, the length () function also needs to be defined as const; otherwise, compilation fails.
Return K> n? K: N;
}

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.