Whimsy and disappointment in implicit type conversion

Source: Internet
Author: User
In C ++, if a type conversion operator or a single parameter constructor without explicit keyword is defined for a custom type (class, the compiler automatically performs implicit type conversion. This is common sense. Whether implicit conversions require rules defined in the compiler is difficult to see, but I understand that one of the meanings of "yes" should be, if no implicit type conversion is performed, the corresponding overload function cannot be found. If yes, the compiler should perform implicit type conversion.

When I saw the "delegate" mode that day, I suddenly had a whimsy and thought of using implicit type conversion to implement a very simple and elegant delegate C ++.

Generally, Class B is responsible for Class A delegation. The code is written in this way, and the constructor is omitted:

  1. Class
  2. {
  3. Public:
  4. F ();
  5. };
  6. Class B
  7. {
  8. A * pA;
  9. Public:
  10. F () {pa-> F ();}
  11. };

However, if Class B wants to proxy many or even all public functions of Class A, repeatedly write functions such as F () {pa-> F ();} function definition is very tiring.

My thoughts at the time were:

  1. # Include <iostream>
  2. Using namespace STD;
  3. Class
  4. {
  5. Public:
  6. Void a_func ()
  7. {
  8. Cout <"A's a_func () is called .";
  9. }
  10. };
  11. Class B
  12. {
  13. A * pA;
  14. Public:
  15. B (): Pa (new ){}
  16. ~ B () {Delete Pa; Pa = 0 ;}
  17. Operator A () const {return * pA ;}
  18. };
  19. Int main ()
  20. {
  21. B;
  22. B. a_func ();
  23. Return 0;
  24. }

Theoretically, the compiler should find that Class B does not have a member function a_func, and that if implicit type conversion is performed, Class A's a_func can be called to perform implicit type conversion. In this way, the "complete" delegation is very lightweight, or the inheritance is not inherited. Of course, this is purely a kind of hack and certainly not the code style that should be recommended in practice.

Unfortunately, GCC and VC 2005 + cannot even compile the above program. This is because the compiler is not smart enough, or because I have incorrect understanding of the C ++ standard. It's a pity that when this sudden whim came, I was so excited that such code could be carried forward for a kind of metaprogramming, in this way, the concept similar to the meta table in Lua is implemented to deal with situations such as method not found.

Unfortunately, I still really want the C ++ standard to clarify the implicit type conversion rules. It is better to add this situation and some more interesting situations ~ P.s., eager to C ++ 0x .....

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.