Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Is this safe? (map)

Is this safe? (map)

Scheduled Pinned Locked Moved ATL / WTL / STL
c++comquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeremy Pullicino
    wrote on last edited by
    #1

    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

    B A 2 Replies Last reply
    0
    • J Jeremy Pullicino

      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

      B Offline
      B Offline
      Brad Sokol
      wrote on last edited by
      #2

      Yes, 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

      1 Reply Last reply
      0
      • J Jeremy Pullicino

        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

        A Offline
        A Offline
        Andrew Walker
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • A Andrew Walker

          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

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          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!

          A 1 Reply Last reply
          0
          • M Michael Dunn

            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!

            A Offline
            A Offline
            Andrew Walker
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • A Andrew Walker

              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

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              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!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups