After reading Objective C ++ Chinese Version 2nd Edition

Source: Internet
Author: User

When I was in college, my school offered C ++ courses. I did a good job. I have been engaged in software development since graduation. I started using VC for projects about a year ago. Recently, after quietly looking at the typical C ++ book Objective C ++, I found that my c ++ level is really average! I have never paid attention to some of the things mentioned in the book! Some of them turned out to be my first hearing!
The following is an example (I replaced the example in the book with a smaller example I wrote, and I marked it in red in the original article ).

1. initialization sequence of Class Members
The title of clause 13 is: the members initialization order in the initialization list should be the same as the declared order in the class.
I don't know if you have paid attention to this issue when using C ++ for development. I never thought about it!
The following is an example:

Class cmyintarray
{
Public:
Cmyintarray (INT lowbound, int highbound );
Size_t getarraysize () const {return data. Size ();};
PRIVATE:
STD: vector <int> data;
Size_t size;
Int lbound, hbound;
};

Cmyintarray: cmyintarray (INT lowbound, int highbound)
: Size (highbound-lowbound + 1)
, Lbound (lowbound)
, Hbound (highbound)
, Data (size)
{
}

If I have not read this book and have no tips, I think many people who are engaged in C ++ development will not be able to see any problems with these simple lines of code (of course, compilation is certainly not a problem ).
However, if the program contains code similar to the following, the problem will pop up when the program is running:

Cmyintarray otest (5, 13 );
Cout <otest. getarraysize () <Endl;

And even if you are tracking in the debug status, it is difficult to find the root cause of the problem!
Where is the problem? In the Declaration Order of member variables! The preceding variable Declaration Order is changed:

Size_t size;
Int lbound, hbound;
STD: vector <int> data;

The problem can be solved!
Surprised? Why? The book provides the answer: the class members system is initialized in the order they are declared in the class; it has nothing to do with the order they appear in the member initialization list.
The root cause of the above Code error is that size is not defined during data initialization! That is to say, to initialize a vector with no defined size, an error will certainly occur.

2. Question about cutting"
The following is a passage in the book (in Clause 22 ):
When a derived class object is handed over as a base class object, all the features that are originally "becoming a derived class Object" will be removed (slicing, only one internal base class object is left.
Below is a small example I wrote:

Class cbase
{
Public:
Virtual void test () const {cout <"output from cbase! "<Endl ;};
};

Class cderived: Public cbase
{
Public:
Virtual void test () const {cout <"output from cderived! "<Endl ;};
};

The following are two functions:

Void test1 (cbase test)
{
Test. Test ();
}

Void Test2 (const cbase & test)
{
Test. Test ();
}

Use the following code to call test1 and Test2 respectively:

Cderived otest;
Test1 (otest );
Test2 (otest );

Question: What are output by calling test1 and Test2 respectively?
The correct answer is:

Output from cbase!
Output from cderived!

Surprised? Not surprising! The book has shown why this is the case: Passing parameters by reference has another advantage: Avoiding the so-called "slicing" problem ".

3. Static binding of non-virtual functions and dynamic binding of virtual functions
The following is an example:

Class cbase
{
Public:
Virtual void test () const {cout <"output from cbase! "<Endl ;};
};

Class cderived: Public cbase
{
Public:
Void test () const {cout <"output from cderived! "<Endl ;};
};

The following code is available:

Cderived D;

Cbase * pb = & D;
Pb-> test ();

Cderived * Pd = & D;
Pd-> test ();

The output is:

Output from cderived!
Output from cderived!

Everyone should know why. However, if you remove the virtual before the test method of cbase, the result will change! The output is:

Output from cbase!
Output from cderived!

At this time, some people will be surprised, Pb and PD point to D, why is it output like this?
The answer is: Non-virtual functions are statically bound, and virtual functions are dynamically bound.
Article 37 In the book provides advice: Never redefine the inherited non-virtual functions.

4. Static binding of default values
Let's look at the example:

Class cbase
{
Public:
Virtual void test (INT itest = 0) const = 0;
};

Class cderived: Public cbase
{
Public:
Void test (INT itest = 1) const {cout <itest <Endl ;};
};

The following is the call code:

Cbase * P = new cderived;
P-> test ();

Our intention is to output 1, but the result is 0!
Don't be surprised. The book provides the answer: the virtual function system is dynamically bound (dynamically bound), while the default parameter value is static binding (statically bound ).
Furthermore, article 38 in the book also gives you advice: Never redefine the inherited default parameters.

Conclusion:In fact, VC and BCB can only be c ++ development tools, and the foundation is C ++. Even if your VC and BCB are more skillful, it is just a class library provided by it, without truly understanding the essence of C ++. These are my thoughts after reading this book. Now, I feel that I have benefited a lot from this book. I hope this article will give readers some inspiration. My next goal is to read the C ++ programming language, the classic book Bjarne stroustrup, the father of C ++. If you have any gains, I will share them here.

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.