---objects and classes of C + + learning notes

Source: Internet
Author: User

Reference from the C + + Primer Plus 6th Edition and the C + + Primer 5th Edition

(The knowledge point in this blog is more scattered and basic, mainly used in my study review)

Access the public member of the created object

1. Automatic Storage Class object: "Period method"

2. Dynamic storage class object (using new): We created an anonymous object and passed the object's address to a pointer. We can use the pointer's pointing character, "-" to access the public member, or you can use "*" to dereference the pointer, and then use the "period method" to access the public member variable.

e.g.

New  Stock; // dynamically creates a stock object that assigns the object's address to pointer a A->show ()  //  accepted(*a). Show ()   //  accepted

3. Static storage Class object: "Period method"

4. Temporary object: "Period method"

Creating a syntax for an object can lead to two types of understanding of the compiler

e.g. Stock stock = stock ();

In this statement, according to the C + + standard, the compiler has two possible ways to execute this syntax:

1. Interpret the statement as: stock stock;

2. First create a temporary object for the stock class, and then copy the temporary object to the stock

Const member functions

If you create a const-decorated class object, it is forbidden to call a member function that is not modified with const

Because, you cannot assure the compiler that your function body of this member function will not modify the object that calls it

For example, you define a const-modified variable and a function in which one of the arguments is a reference to the same type.

If you want to pass this const variable as this parameter to the function, then this reference to the formal parameter list is to be modified with a const so that the constant is not modified.

e.g.

void Test (constint &"Hello World!\n";} int Main (intChar *argv[]) {  constint5  ;  Test (a);  return0;}

But the problem is, this example can not be used to solve the problem, because the object calls the member function when the time, you can not explicitly as a parameter to the member function of the formal parameter list! When an object directly calls a member function or an object pointer indirectly invokes a member function, it passes the address of the real object to the hidden parameter in the member function, the this pointer. and access to member variables in member functions is accessed through the this pointer. The crux of the problem is: how do you guarantee that the member variables in this object will not be modified during the execution of the function?

C + + introduces a syntax to solve this problem: Place the const keyword behind the function's formal parameter list. Even if you are not careful, in the function body to write the modification of the object member variable operation, the compiler will be well-intentioned to remind you.

From the above, we can summarize a good programming habit: According to the "C + + Primer plus 6th Edition" in the exact words-"just as much as the const reference and pointers as function row parameters, as long as the class method does not modify the calling object, it should be declared as const", This is a good programming practice, regardless of whether the object being created uses the const modifier.

Just like this:

void Const;

This is the time of the Declaration. When implemented, put the const behind the formal parameter list and the curly braces in the function body.

void Const {/  *  implementation*/}

---objects and classes of C + + learning notes

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.