C + + static binding and dynamic binding------never redefine inherited default parameters

Source: Internet
Author: User

Before you understand static and dynamic bindings, learn what the static type of the object is and what is the dynamic type of the object.

    • Static type of object: the type that the object takes when declaring. is determined by the compiler.
    • Dynamic type of object: The type of object currently being referred to. is determined at run time.
Dynamic types can be changed, and static types cannot be changed. See an example
  
 
  1. class Base
  2. {
  3. public:
  4. void setData(int i=10)
  5. {
  6. cout <<" virtual int Base::setData()"<<endl;
  7. }
  8. virtual int getData()
  9. {
  10. cout <<" virtual int Base::getData()"<<endl;
  11. }
  12. private:
  13. int m_value;
  14. };
  15. class Derive: public Base
  16. {
  17. public:
  18. void setData(int i=20)
  19. {
  20. cout <<" virtual int Derive::setData()"<<endl;
  21. }
  22. virtual int getData()
  23. {
  24. cout <<" virtual int Derive::getData()"<<endl;
  25. }
  26. };
  27. int _tmain(int argc, _TCHAR* argv[])
  28. {
  29. Derive *pd = new Derive;//pd的静态类型为Derive,动态类型也为Derive
  30. Base *pb = pd; //pb的静态类型为Base,动态类型为Derive
  31. return 0;
  32. }
Figure out what is the static type of the object and what is the dynamic type of the object. Below is a description of what is static binding and what is dynamic binding.
    • Static binding: The bound object is a static type. An attribute depends on the static type of the object, which occurs at compile time.
    • Dynamic binding: The bound object is a dynamic type. There are attributes that depend on the dynamic type of the object, which occurs at run time.
Only virtual functions use dynamic bindings, and all others are static bindings.
We use PB,PD to call the non-virtual function separately,
  
 
    1. derive * pd = new derive Span class= "pun";
    2. base * PB = PD ;
    3. PB -> setdata
    4. pd -> setdata
Output:
Because SetData is statically bound, the compiler chooses the function at compile time based on the static type of the object.The static type of PB is base,pd static type is derive.
we use PD,PB to call virtual functions separately
Because GetData is a virtual function, it is dynamically bound. Dynamic types are derive*. The function of the base class and the function of the derived class are called separately.Note: Such a design is particularly bad, and a name masking occurs between the derived class and the base class. This is only written for static binding and dynamic binding. If the derived class and the base class are is-a relationships, do not inherit a non-virtual function under any circumstances.
Note: The dynamic type of pointers and references may be inconsistent with static types, but the static type of the object is consistent with the dynamic type. Derive d. D.setdata () D.getdata () is called a member function of a derived class.
When the virtual function has default parameters, the situation becomes a bit more complicated. Because the default parameter takes a static binding.
  
 
  1. class Base
  2. {
  3. public:
  4. virtual void getData(int i=10)
  5. {
  6. cout <<" virtual int Base::getData()" << i <<endl;
  7. }
  8. };
  9. class Derive: public Base
  10. {
  11. public:
  12. virtual void getData(int i = 20)
  13. {
  14. cout <<" virtual int Derive::getData()" << i <<endl;
  15. }
  16. };
  17. int _tmain(int argc, _TCHAR* argv[])
  18. {
  19. Derive *pd = new Derive;
  20. Base *pb = pd;
  21. pb->getData();
  22. pd->getData();
  23. return 0;
  24. }
Output:
Although the default parameters are called, the default parameters are static bindings. That's why it's going to get that way.to prevent this from happening: effective C + + specifically points out a:the inherited default parameters are never redefined. ToOne way to avoid this is to use NVI (Non-virtual interface): Declare a common non-virtual function in the base class and give the vacancy province parameter (because it is a static binding) in which the private virtual function is called, so that when the derived class method is called. Because a common non-virtual function takes a static binding, the derived class must inherit the non-virtual function. As a result, when the function is called, the default parameters are always available.

From for notes (Wiz)

C + + static binding and dynamic binding------never redefine inherited default parameters

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.