Home > Back-end >  Should not be: call the function is the actual point to a member function of the type of an object,
Should not be: call the function is the actual point to a member function of the type of an object,

Time:10-18

The class Base
{
Public:
Virtual void f (float x) {cout & lt; <"Base: : f (float)" & lt; Void g (float x) {cout & lt; <"Base: : g (float)" & lt; Void h (float x) {cout & lt; <"Base: : h (float)" & lt; };

The class Derived: public Base
{
Public:
Virtual void f (float x) {cout & lt; <"Derived: : f (float)" & lt; Void g (int x) {cout & lt; <"Derived: : g (int)" & lt; Void h (float x) {cout & lt; <"Derived: : h (float)" & lt; };
Int main ()
{
Derived d;
The Base * pb=& amp; d;
Pb - & gt; F (3.14 f);//Derived: : f (float) 3.14
Pb - & gt; G (3.14 f);//Base: : g (float) 3.14
Pb - & gt; H (3.14 f);//Base: : h (float) 3.14
return 0;
}

Should not be: call the function is the actual point to a member function of the type of an object, rather than a member function pointer type? How to g and h the base class of functions,

CodePudding user response:

The
refer to the original poster shff11 response:
class Base
{
Public:
Virtual void f (float x) {cout & lt; <"Base: : f (float)" & lt; Void g (float x) {cout & lt; <"Base: : g (float)" & lt; Void h (float x) {cout & lt; <"Base: : h (float)" & lt; };

The class Derived: public Base
{
Public:
Virtual void f (float x) {cout & lt; <"Derived: : f (float)" & lt; Void g (int x) {cout & lt; <"Derived: : g (int)" & lt; Void h (float x) {cout & lt; <"Derived: : h (float)" & lt; };
Int main ()
{
Derived d;
The Base * pb=& amp; d;
Pb - & gt; F (3.14 f);//Derived: : f (float) 3.14
Pb - & gt; G (3.14 f);//Base: : g (float) 3.14
Pb - & gt; H (3.14 f);//Base: : h (float) 3.14
return 0;
}

Should not be: call the function is the actual point to a member function of the type of an object, rather than a member function pointer type? How to g and h is the base class function,

This sentence is wrong, into so that it can be:
For virtual function, call the function is the actual point to a member function of the type of an object, rather than the member function pointer type,

CodePudding user response:

G h is not a virtual function, then the Base pointer call g h is the Base of g h, this is a compile-time binding function; Only virtual function call object instance corresponding subclass function, this is the run-time binding function, such as f,

  • Related