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. C / C++ / MFC
  4. Invisible temporary

Invisible temporary

Scheduled Pinned Locked Moved C / C++ / MFC
comdata-structurestoolsjsonhelp
1 Posts 1 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.
  • B Offline
    B Offline
    Bob Stanneveld
    wrote on last edited by
    #1

    Hello, I have the following problem. I want to create a temporary object that is transparent to the caller of the function which creates the temporary. We all know that values returned by value from a function, the temporary is created on the stack until it is no longer needed. I wan't to use this property of the temporary to create a locking / unlocking mechanism for an object. See the following code for details

    // this class will be used by the user of the API
    // In the actual code it will be a template parameter.
    class CArbitraryClass
    {
    public:
    void DoFoo() {}
    };

    // this is the wrapper that the user uses to store CArbitraryClass objects
    class CWrapperClass
    {
    public:
    // will return a temporary object which the user doesn't know or sees
    CInvisibleTemporary operator->()
    {
    return CInvisibleTemporary::CInvisibleTemporary();
    }
    };

    // this object is the 'invisible' temporary. It should pass on the
    // stored object (CArbitraryClass). The user should not have to
    // call any extra functions for it.
    class CInvisibleTemporary
    {
    public:
    CArbitraryClass& operator->() { return m_ArbitraryObject; }

    private:
    CArbitraryClass m_ArbitraryObject;
    };

    So the code should look like the following statements: CWrapperClass Wrapper; Wrapper->DoFoo(); But it looks like: CWrapperClass Wrapper; Wrapper.operator ->().operator ->().DoFoo(); Am I trying to do something impossible? Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

    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