Declare the discussion of the virtual function

xiaoxiao2021-03-06  64

http://blog.9cbs.net/snaill/archive/2004/09/25/116287.aspx

Some people think that "virtual is nothing to use for multi-state declarations for private", "Private: The visible domain is the class itself, derived class is invisible!"

I don't think so, but after I did experiment, I found that it is not that it is not such a thing (code environment VC6.0)

Interface ITEST {private: Virtual void test () = 0;

Class Ctest: Public ITest: PUBLIC ITEST

If you don't define Test member functions in ctest, the fundamental compilation is not

If defined, just call the method :: test method, use ctest :: test, still can be called

So even if private is defined, it is still possible to implement a polymorphism in the derived class.

So, is this like a function of pureness? Have the following experiments:

Class itest {public: void test2 () {test ();} private: virtual void test () {};

Class Ctest: Public ITest: PUBLIC ITEST

The call is as follows: ITEST * PTEST = (iTest *) this; PTEST-> TEST2 ();

If you implement Test functions in Ctest, CTest :: test will still be called

Visible subclasses can not directly call the PRIVATE function of the parent class, but can be overloaded, and the constraints of the function can be modified (change from private to public).

Someone asked: "The polygon is not the function of the" base class "or the reference call to send the function", since it is impossible to call CTest :: test from ITEST, how to call 'to implement polymorphism in derived class? "?"

I think this is a relatively extreme idea, such as the above example, some of the code in Test2 can be implemented with Test, I can use the derived class to make Test polymorphism, compiler support, but only the polymorphism Category.

Finally, thanks 9CBS.NET's zhph (seeking new) to put forward this problem, do not tell the experiment really think about it, please organize your answer.

转载请注明原文地址:https://www.9cbs.com/read-113141.html

New Post(0)