Note of using static_cast

Source: Internet
Author: User

# Include "stdafx. H"
# Include <assert. h>

Class iinterface1
{
Public:
Virtual void vfunc1 () = 0;
};

Class iinterface2
{
Public:
Virtual void vfunc2 () = 0;
};

Class testclass1:
Public iinterface1, public iinterface2
{
Void vfunc1 ();
Void vfunc2 ();
};

Void testclass1: vfunc1 ()
{
;
}
Void testclass1: vfunc2 ()
{
;
}

Class interface12:
Public iinterface1, public iinterface2
{

};
Template <class intttt>
Intttt * makeinterface2 (iinterface1 * pinterface1)
{
Return static_cast <intttt *> (static_cast <interface12> (pinterface1 ));
}

Int _ tmain (INT argc, _ tchar * argv [])
{
Testclass1 * ptestclass1 = new testclass1 ();
Iinterface1 * pinterface1 = dynamic_cast <iinterface1 *> (ptestclass1 );
Assert (pinterface1 );
Iinterface2 * pinterface2 = static_cast <iinterface2 *> (pinterface1 );
Iinterface2 * pinterface2 = makeinterface2 <iinterface2> (pinterface1 );
Return 0;
}

The red line code will cause compile error:
Error c2440: 'static _ cast': cannot convert from 'iinterface1 * 'to 'iinterface2 *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

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.