Be wary of using class construction and destructor to do resource allocation release, use of temporary variables

Source: Internet
Author: User

There is a class

Class Cmiregularpath
{public
:
    cmiregularpath (LPCTSTR lppath);
    ~cmiregularpath ();

    operator LPCTSTR ();
    operator LPTSTR ();

Protected:
    LPTSTR M_lppathbuffer;
    LPCTSTR M_lppath;
    LPCTSTR m_lppathregular;
};

Here is the call to

void Func1 (LPTSTR lppointer)
{

}

void Func2 (LPCTSTR lppointer)
{

}

void Test
{
    func1 (Cmiregularpath) (_t ("D:\\aa/as"));
    Func2 (Cmiregularpath) (_t ("D:\\aa/as"));
    LPTSTR lpP1 = Cmiregularpath (_t ("D:\\aa/as"));
    LPCTSTR lpP2 = Cmiregularpath (_t ("D:\\aa/as"));

The truth

The first two calls in the test function are correct, the right string is available in func1 and Func2, the latter two are wrong, the class destructor is executed immediately after the line is executed, so LPP1,LPP2 does not point to the true string.

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.