C + + When the method of a subclass is the same as the method of the parent class ...

Source: Internet
Author: User

C + + When the member function of a subclass has the same name as the member function of the parent class:

If the parent class does not have a virtual declaration, the subclass function can be different from the parent class function return value, but the function name and argument list must be the same.

The subclass instance passes the "." Called a function of a subclass

If a subclass wants to call a function of the parent class, it needs to use the subclass instance. Parent class:: The form of the function name ().

The parent class instance passes the "." Called a function of the parent class

Parent class cannot call a function with the same name as a subclass

If called by pointers

The type of the pointer determines which class's function is called

The parent class address is cast to a child class pointer and can also call a function of a subclass.

If the parent class has a virtual declaration,

The virtual function fixed the form of the function, so the return type of the subclass function and the parent function must be identical, otherwise error:conflicting return type specified for ' virtual int cderive::show () '

Then pass the "." When called,

The subclass instance calls the subclass function, and the parent class instance calls the parent class function.

If called by,

Polymorphism, that is, when the use of the base class pointer through the call, depending on whether the contents of the pointer is a base class address or a subclass address, respectively, call the corresponding function. The subclass address is assigned to the parent class pointer, and the child class is called by default, but can be passed through the parent class pointer, parent class: function name () A function that explicitly invokes the parent class.

When called with a subclass pointer, the parent class address is coerced to a child class pointer, and the function that is still its intrinsic function, the parent class, can be called by default to call a function of the subclass by the subclass pointer, the subclass name: function name ().

When you point a subclass pointer to the child class instance address, the subclass function is called by default, and the parent class's function can be called by the child class pointer, parent class name: function name ().

When a parameter is different, it is two functions,

The Invoke function at this point depends on the type of the pointer variable that determines which function is called, regardless of its actual value.

A pointer to a subclass type can force a call to the parent class function by means of the parent class name: function name (), which is the child class pointer.

C + + When the method of a subclass is the same as the method of the parent class ...

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.