Effective C + + reading notes clause 36-37

Source: Internet
Author: User

Article 36: Never redefine inherited non-virtual functions

Important point: The Non-virtual function is statically bound

1241.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream>using namespace Std;class base{public:void func () {cout<< "Base:: Func () "<<endl;}}; Class Derived:public base{public:void func () {cout<< "Derived::func ()" <<endl;}}; int _tmain (int argc, _tchar* argv[]) {Derived test; base* PB = &test;derived* PD = &test;pb->func ();//Call Base::func () Pd->func ();//Call Derived::func ()/* Here we are not talking about hidden problems, the above PB points to a subclass, but the call is the Func () function of the parent class, as for why, because it is not a virtual function, there is no virtual; to understand one thing: the Non-virtual function Base::func and Derived: : Func is statically bound, PB is declared as a person pointer-to-base, the non-virtual function that is called by PB is always the version defined by Base, even though PB points to an object of a derived class of its type. PD calls the derived version of the function, which can also be said to be hidden, but the end result is static binding. General  Derived Dtest,dtest.func () This call we generally say is because of the hidden. For objects, and we have pointers or references in this case because of static bindings! */getchar (); return 0;}


Total: In no case should we redefine an inherited non-virtual function;

For a base class, if a function is defined as a non-virtual function, then it means that the function does not want to change, so the subclass should not inherit it, the non-virtual function in a class, the invariance overrides its specificity.

Article 37: Never redefine inherited default parameter values:

Virtual function is dynamic binding, the default parameter value is static binding;

//1240.cpp: Defines the entry point for the console application. #include "stdafx.h" #include <iostream>using namespace Std;class shape{public:enum Shapecolor{red,green,blue} ; virtual void Draw (shapecolor color = Red) const = 0;}; Class rectangle:public shape{public:virtual void Draw (shapecolor color = Green) const{cout<< "Rectangle" << Endl;}}; Class circle:public shape{public:virtual void Draw (shapecolor color) const{cout<< "Circle" <<endl;}}; int _tmain (int argc, _tchar* argv[]) {shape* ps; shape* pc = new Circle; shape* PR = new Rectangle;pr->draw ();//This is called the Rectangle draw function, but the function parameter color is the parameter inside the shape parent class/*virtual function is dynamically bound, The default parameter values are static bindings. This occurs when the above call a virtual function defined in the subclass, while using the base class as the default parameter value specified by it. The dynamic type of PR is rectangle*, so call is ractangle virtual function, but because the static type of PR is shape*, so the default parameter value of this function call is from the fish Shape Class.*/getchar (); return 0;} 


Summary: Do not redefine an inherited default parameter value because the default parameter value is statically bound, and the virtual function-the only thing you should overwrite-is dynamic binding!!

Effective C + + reading notes clause 36-37

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.