C ++'s father, learning notes (4) -- many things of the class

Source: Internet
Author: User

 

Class. There are too many tasks. Let's try to figure it out.

A class is a user-defined type. The goal of the class concept in C ++ is to provide a new type of tool for programmers, which can be used as easily as internally.

Access control: the default access method for class members is private. A struct is also a class, but the default method of its members is public. Non-member functions are not allowed to access private members.

Constructor: a function with the same name as a class and no return value. Everyone knows this. It's easy. The default constructor is a constructor that does not need to provide parameters during the call. If the user declares a default constructor, it will be used; otherwise, if necessary, and the user does not declare other constructor, the compiler will try to generate one. The default constructor generated by the compiler is implicitly a member of the class type and the default constructor related to its base class call. The Class type refers to the types generated by the classes defined by the programmer, so as to distinguish it from other user-defined types. I believe there is no problem here. Note that const and reference must be initialized. classes that contain const or reference members cannot be constructed by default unless the programmer provides the default constructor. Example: struct X

{

Const int;

Const int & r;

};

X x; // error; X no default constructor can also display the call. The internal types also have Default constructors.

Next, let's talk about the replication constructor. Let's take a look at how the replication constructor is introduced.

According to the default conventions, class objects can be copied. In particular, you can use the object and copy of a class to initialize other objects of the class. Even if you declare the constructor, you can do this: Date d = today; // by default, the copying of class objects is the replication of each member. If a Class X does not require this default method, you can define a replication constructor X: X (const X &) to provide the required behavior. Another concept is the copy assignment, which is easy to mix with the copy constructor. Let's figure them out together. First look at a program: void h ()

{

Table t1;

Table t2 = t1; // copy Initialization

Table t3;

T3 = t2; // copy value assignment

} It seems that there is no problem. The interpretation method mentioned above may have an unexpected effect when it is applied to objects of classes with pointer members. For objects that contain resources managed by Constructors/destructor, the semantics of copying by members is usually incorrect. Here, the default constructor of Table is called once for t1 and t3 each time, which is twice in total. However, the Table destructor are called three times! Since the default value assignment is based on the member assignment, at the end of h (), t1, t2, and t3 will each contain a pointer, they all point to the name array allocated from the free storage when t1 is created. The pointer to the array allocated when t3 is created is not retained because it is overwritten by the value t3 = t2. In this way, if there is no automatic waste collection, the storage of the array will be lost for this program. On the other hand, the array allocated for the creation of t1 will be deleted three times because it appears in both t1, t2, and t3. In this case, the results are undefined and may be disastrous. This exception can be avoided by clearly defining the meaning of Table replication: class Table

{

//---

Table (const Table &); // copy the constructor

Table & operator = (const Table &); // copy the assignment

}. We can define what we think is the most appropriate for these replication operations, for example

// Add the detailed definition of the Table class here

Class Table

{

Name * p;

Size_t sz;

Public:

Table (size_t s = 15)

{

P = new Name [sz = s];

}

~ Table ()

{

Delete [] p;

}

Name * loopup (const char *);

Bool insert (Name *);

}

 

Table: Table (const Table & t) // copy the constructor

{

P = new Name [z = t. sz];

For (int I = 0; I <sz; I ++)

P [I] = t. p [I];

}

 

Table & Table: operator = (const Table & t) // assign a value

{

If (this! = & T) // caution: t = t

{

Delete [] p;

P = new Name [sz = t. sz];

For (int I = 0; I <sz; I ++)

P [I] = t. p [I];

}

Return * this;

} This is almost always the case. The copy constructor and the copy assignment are usually quite different. The root cause is that the replication constructor initializes the initialized storage zone, and the copy assignment operator must correctly process an object with a good structure.

Member constants:

For those static integer members, you can add a constant expression to its member declaration as the initial expression, for example

Class Curious

{

Static const int c1 = 7; // OK, but remember to define

Static int c2 = 11; // error: Non-const

Const int c3; // error: non-Static

Static const int c4 = f (1); // error: the initial expression in the class is not a constant

Static const float c5 = 7.0; // error: the value initialized in the class is not an integer.

} 1) const cannot be used to create constants in the class! Because: the class only describes the object form and does not really create an object! Therefore, there is no storage space before the object is created!

2) const is used to create constants! Method 1 You can use enumeration:

Class

{

Enum {buf_size_t buf_size =,} // create a constant with enumeration, but not a data member

 

}

Method 2 You can use static

Class

{

Private:

Static const buf_size_t buf_size = 30; // The constant is stored together with the static constant instead of in the object.

} But the C ++ programming language book says that when you use an initialized member and need to store it as an object in the memory, this member must be defined somewhere. The initial formula does not need to be repeated: const int Curious: c1; // required, but it does not need to be repeated here

Const int * p = & Cusious: c1; // OK: Curious; // required, but do not need to repeat the initial line here? It is also necessary. There is a problem after testing-the definition in the current range or the re-statement is invalid. Is there any other reason for the wrong book?

Please kindly advise

Let's get here today. There is a problem here and we still need to think about it.

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.