Is this safe? (map)
-
Is the following safe?
{ map<CComBSTR, CComBSTR> mp; { CComBSTR& bstrNew = mp[L"1"]; bstrNew = "Hello"; } wprintf(mp[L"1"]); }
Will bstrNew get destroyed, thus invalidating the "Hello" string? Jeremy Pullicino C++ Developer Homepage -
Is the following safe?
{ map<CComBSTR, CComBSTR> mp; { CComBSTR& bstrNew = mp[L"1"]; bstrNew = "Hello"; } wprintf(mp[L"1"]); }
Will bstrNew get destroyed, thus invalidating the "Hello" string? Jeremy Pullicino C++ Developer HomepageYes, this is safe. A reference is an alias for an object that exists elsewhere - in the map, in this case. When a reference goes out of scope, the referenced object still exists and no destructor call is made. In this respect, references work like pointers. Brad
-
Is the following safe?
{ map<CComBSTR, CComBSTR> mp; { CComBSTR& bstrNew = mp[L"1"]; bstrNew = "Hello"; } wprintf(mp[L"1"]); }
Will bstrNew get destroyed, thus invalidating the "Hello" string? Jeremy Pullicino C++ Developer HomepageJeremy Pullicino wrote: map<CComBSTR, CComBSTR> mp; I'd be careful, mixing non standard-library components with standard components could be dangerous. If you can afford to convert and store std::string's / std::wstring's, or boost::shared_ptr's to BSTR's prefer that to using the microsoft wrappers. From MSDN CComBSTR::operator & BSTR* operator &( ); Modern C++ Design (7.6) - Alexandrescu "Overloading unary operator& makes the Smart Pointer unusable with STL containers." He then goes on to suggest never overloading operator& for components that are going to be used generically.
If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling
-
Jeremy Pullicino wrote: map<CComBSTR, CComBSTR> mp; I'd be careful, mixing non standard-library components with standard components could be dangerous. If you can afford to convert and store std::string's / std::wstring's, or boost::shared_ptr's to BSTR's prefer that to using the microsoft wrappers. From MSDN CComBSTR::operator & BSTR* operator &( ); Modern C++ Design (7.6) - Alexandrescu "Overloading unary operator& makes the Smart Pointer unusable with STL containers." He then goes on to suggest never overloading operator& for components that are going to be used generically.
If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling
Andrew Walker wrote: "Overloading unary operator& makes the Smart Pointer unusable with STL containers." That's what
CAdapt
is for:std::list< CAdapt<CComBSTR> > list_of_bstrs;
--Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Ericahist updated (again) Sep 6! -
Andrew Walker wrote: "Overloading unary operator& makes the Smart Pointer unusable with STL containers." That's what
CAdapt
is for:std::list< CAdapt<CComBSTR> > list_of_bstrs;
--Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Ericahist updated (again) Sep 6!Cool, thanks mike didn't even know it existed. When did this get added? for .NET
If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling
-
Cool, thanks mike didn't even know it existed. When did this get added? for .NET
If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling
I don't know when it was added, but it's in VC 6 (which is all I care about ;)) --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Ericahist updated (again) Sep 6!