C + + Subclass functions overwrite the parent class function with the same name __jquery

Source: Internet
Author: User

Class B

{

Public

void f (int) const

{

cout << "B.int" << Endl;

}

void f (double) const

{

cout << "b.double" << Endl;

}

};

Class D:public B

{

Public

void f (void*) const

{

cout << "d.void" << Endl;

}

};

int main ()

{

D D;

D.F (0); Call that function.

D.F (1); Call that function.

D.F (0.1); Call that function.

}

Answer: All 3 calls to the subclass 's F (void*). However, because the latter two cannot be converted to void*, a compilation error occurred. Void* is the one that can point

Any type of pointer . In C + +, only int 0 can be converted to a pointer type , and the rest is not possible, so there is an error.

About the override of a subclass function on a parent class function:

In C + + class, there are two concepts, one is overload , one is overwrite .

overloads exist only within the class and are not transmitted between the parent class and the subclass. This is true even if a function is declared as virtual.

If a class has the same function as the parent class, then the class will overwrite the method of its parent class , unless you force it at the time of the call

conversion to the parent class type , otherwise an attempt to make a similar overload to the subclass and the parent class is unsuccessful.

Class B

{

Public

void f (int i) const

{

printf ("B::f (int):%d\n", i);

}

virtual void F (double D) const

{

printf ("B::f (double):%f\n", D);

}

};

Class D:public B

{

Public

void f (void* p) const

{

printf ("B::f (void*):%x\n", p);

}

void f (int i) const{

printf ("D::f (int):%d\n", i);

}

};

int main (int argc, char* argv[])

{

D D;

((b*) &d)->f (0); Force call, otherwise the F (int) of the parent class is not visible to the subclass

D.F (1);

((b*) &d)->f (1); //

((b*) &d)->f (0.1); //

D.F (0.1); Warning:conversion from ' const double ' to ' int ', possible loss of data

D.F (NULL); Warning:converting NULL to Non-pointer type

return 0;

}

Run Result:

B::f (int): 0

D::f (int): 1

B::f (int): 1

B::f (double): 0.100000

D::f (int): 0

D::f (int): 0

So, for pure virtual functions , the reality is the same. For example

Class X

{

Public

virtual void f (int) = 0;

};

Class Y:public X

{

Public

virtual void f (int n)

{

printf ("Y::f (int):%d\n", N);

}

virtual void F (double D)

{

printf ("Y::f (double):%f\n", D);

}

};

Class Z:public Y

{

Public

virtual void f (int n)

{

printf ("Z:f (int):%d\n", N);

}

};

int main ()

{

Z Z;

Z.F (1);

Z.F (0.1);

}

Run Result:

Z:f (int): 1

Z:f (int): 0

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.