Invisible temporary
-
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[^]