Several small issues worth analyzing in C + + (1)

Source: Internet
Author: User

here are 3 small questions that I think C + + beginner should be able to answer or discern clearly. I hope we can dig more information through the topic, not just to solve the problem. My favorite words: Ability is limited, so as a discussion, hope to discuss together, point out the mistake.

In addition, I have encountered a problem that I feel necessary to record, write down and say, so each piece of content may not be a single topic.

1, first to see a simple topic. There is the following inheritance class:

classperson{ Public:    voidWalk ()//The "Walk" of ordinary people{cout<<"Person::walk I am an ordinary people."<<Endl;        }; };classStudent: Publicperson{ Public:    voidWalk ()//The student's "Go"{cout<<"Student::walk I am a Student."<<Endl;        }; };

you're not mistaken . Walk () is a non-virtual function. Please explain the following code:

Student S; person * pp = &s;pp-Walk (); Student* ps= &s;ps->walk ();

The result is this:

Analysis: Walk () is a non-virtual function that is limited by static binding, so what type of PP and PS determines the version of the call. Here, I would also like to explain that: understand interface inheritance and implementation inheritance. The purpose of declaring a non-virtual function is to make the interface of the derived class inheritance function and a mandatory implementation . Therefore, never redefine the inherited non-virtual function .

2, The following problem is essentially static binding and dynamic binding problems, but it does not seem so obvious.

classshape{ Public:    enumShapecolor{red, Green, Blue};//Shape Color    Virtual voidDraw (shapecolor color = Red)Const=0;};classCircle: Publicshape{ Public:    Virtual voidDraw (shapecolor color)Const{cout<<"I am Circle::D Raw."; cout<<"My color ="<< Color <<Endl; }};classRectangle: Publicshape{ Public:    Virtual voidDraw (shapecolor color = Green)Const            //The default parameter value is changed{cout<<"I am Rectangle::D Raw."; cout<<"My color ="<< Color <<Endl; }};

I would like to say two main questions.

(1) when you invoke the following, please explain what will happen.

Circle CR;            // (1) compilation does not pass Cr. Draw ();             *ps = &cr;    // (2)Ps->draw ();

Yes, (1) calling through an object without specifying a parameter is an error, and (2) The result is this: color = 0 for red this you should know.

parsing: A static binding through an object invocation is a must to specify the parameter value because the static binding function does not inherit the default parameter value from the base class. Dynamic binding can inherit parameter values from base class. Note that I don't emphasize the concept of dynamic binding and static binding here, but the following must be a static binding:

*ps = &cr;    // this is still statically bound, static type Circle *, compilation does not pass Ps->draw ();

(2) The second question I want to say, please explain the result of the call below.

New rectangle;ps1-Draw (); ShapeNew  circle;ps2->draw ();

Is such a gratifying result:

You mean, you've changed the default value of draw to 1 (Green) in rectangle, how is it not working?

Analysis: Rectangle: The default parameter value for:D Raw is green, but the static type of PS2 is shape*, so the default parameter value for this call comes from the shape class.

If you have to let rectangle::D raw parameters change, you can call (provide parameters) like this:

New rectangle;ps4-Draw (Shape::green); ShapeNew  circle;ps5->draw (Shape::green);

the question is to remind you: The virtual function is a dynamic binding, and the default parameter value is a static binding. Therefore, this default parameter value should not be redefined.

3. Why does multiple inheritance contain multiple virtual table pointers instead of one?

This problem is I see a classmate interview experience, the interviewer mentioned, I try to answer, do not know in the idea, also please add and correct.

A: Multiple inheritance, because the compiler implemented a derived class n-1 virtual table, n represents the number of the previous base class , of course, assuming that each base class has at least one virtual function, Otherwise the compiler will not add vptr and vtbl for it. So how many virtual tables there are, naturally, how many pointers point to, not one.

I do not know this is reasonable unreasonable, perhaps the interviewer asked the point is "why need more than a virtual table?" A dummy table row? ”

This is what compiler vendors do, and standards are not standardized. C + + 's father has made such a compiler prototype, by increasing the volume of the VTBL, there is not only one pointer on each slot, there is an offset, which is used to adjust the direction of this pointer.

The disadvantage of this is that all virtual function pointers in the VTBL contain such an offset, and assume that the this point is not to be adjusted, or that the addition of offset is to be called, although the offset is now 0. In addition, the volume of each slot in the VTBL expands. These are efficiency issues.

In fact, the point used to adjust this is more trunk technology, which must be compiled in order to achieve high efficiency. In addition, the Sun compiler is to chain a number of virtual tables into 1, each table containing the next table pointer (by means of offset), so you need a pointer.

Understanding ability is limited, do not know to ask is not such a thing?

Several small issues worth analyzing in C + + (1)

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.