Using smart pointers in "C + +" Vs2008 __c++

Source: Internet
Author: User

The TR1 module is the c++11 of the formal birth of the new standard and has been determined to incorporate the C + + feature. VS2008 is the C++11 standard on the eve of the formal birth, the c++03 and TR1 standard support for the best of the IDE, is quite a classic version of many programs, the development and maintenance of the use of vs2008. Vs2008 to use the TR1 standard, you need to install the SP1 patch; Use the TR1 standard in your code, and you need to add TR1:: prefix, such as std::tr1::shared_ptr. Compilers that conform to the C++11 standard can be used directly, such as Std::shared_ptr.

VS2008SP1 's TR1 module has added features such as smart pointers, regular expressions, hash tables, random number generators, and so on. The most commonly used smart pointer is std::tr1::shared_ptr.

Smart pointers and common pointers are similar in polymorphism, and can be implicitly converted from derived class pointers to base class pointers (up-conversion), and cannot be automatically transferred from a base class pointer to a derived class pointer (downward conversion). The normal pointer is converted up and down through the dynamic_cast function implementation, and the smart pointer implements the conversion through Dynamic_pointer_cast.

Examples are as follows:

struct a{virtual void print () {printf ("a\n");};
struct b:public a{virtual void print () {printf ("b\n");};
typedef std::tr1::shared_ptr<a> BASEPTR;
 
typedef std:: tr1::shared_ptr<b> derivedptr;
void Foo1 (A * A) {printf ("foo1:"); A->print ();}
void Foo2 (b* B) {printf ("Foo2:"); B->print ();}
void Foo3 (Baseptr a) {printf ("Foo3:"); A->print ();}

void Foo4 (Derivedptr b) {printf ("Foo4:"); B->print ();}
    int main () {* BP1 = new B;
    b* DP1 = new B;
    Baseptr bp2 =derivedptr (new B);
    Derivedptr DP2 =derivedptr (new B);
    Foo1 (BP1); Foo2 (BP1);//base class pointers cannot be automatically converted to derived class pointer//foo2 (dynamic_cast<b> (BP1));//dynamic_case function Implementation type conversion, B pointer cannot convert to B object Foo2 (
    Dynamic_cast<b*> (BP1));
    Foo1 (DP1);
 
    Foo2 (DP1);
   Foo3 (BP2); Foo4 (dynamic_cast<b> (BP2));//Smart pointers cannot be converted to dynamic_cast//foo4 (dynamic_cast<b*> (BP2));//Smart pointers cannot be used with dynamic _cast Conversion//foo4 (dynamic_cast<derivedptr> (BP2)); Smart pointers cannot be converted with dynamic_cast Foo4 (std::tr1::d ynamic_pointer_cast<b> (BP2));
    Foo3 (DP2);
    Foo4 (DP2);
    Delete BP1; Delete DP1}

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.