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. Managed C++/CLI
  4. Small RAII-like cleanup class in C++/CLI

Small RAII-like cleanup class in C++/CLI

Scheduled Pinned Locked Moved Managed C++/CLI
c++toolshelptutorialquestion
1 Posts 1 Posters 4 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
    John Schroedl
    wrote on last edited by
    #1

    In native code, we often use helper classes to save a reference to a member, set its value and reset it in destruction. standard stuff. Unfortunately, I haven't found a good way in a ref class to do the same and I'm hoping for some tips here. Since I cannot hold a reference to a member of a managed object as a member of another object, I tried using a tracking reference. Unfortunately, that results in an similar error:

    error C3160: 'System::Boolean ^%': a data member of a managed class cannot have this type
    note: an interior reference can never be allocated on the gc heap

    An example I hope to get working...

    ref class Foo { bool m_guard; . . . }

    ref class PushBool sealed {
    bool ^% m_tref;
    public:
    PushBool(bool ^% b, bool newVal) : m_tref(b), m_oldval(b) { b = newVal; }
    ~PushBool() { m_tref = m_oldval; }
    };

    usage...

    void Foo::work() {
    if (m_guard) return;
    PushBool no_reentry(m_guard, true);
    ...
    // Resets m_flag to original value here.
    }

    Do any of the C++/CLI devs here have advice on handling this? I can probably concoct a way to make it work but was hoping for a reusable utility class. John

    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