|
-
May 17th, 2009, 01:36 PM
#1
Temporaries problem (C++)
I have some code that is like this:
Code:
(input is a stream &)
[...]
stream *uncompStream;
if (compressed == false)
{
uncompStream = &input;
}
else
{
uncompStream = new uncompress_stream(&input);
}
[...]
Is there any way to write this code without dynamically allocating the uncompress_stream class?
I can't find any way to allocate the uncompress_stream in the stack without it being destroyed in the "}".
The only "automatic" solution that I've found is to use a smart pointer (assigned to NULL initially), and use it if it I need an uncompress_stream. However, that looks a bit bad.
-
May 17th, 2009, 01:42 PM
#2
Re: Temporaries problem (C++)
It does sound like dynamic memory allocation is the correct approach here, particularly with a smart pointer.
-
May 18th, 2009, 03:05 AM
#3
Re: Temporaries problem (C++)
You can use boost/tr1 here but you need a custom deleter to do nothing with the one passed in as a parameter.
You can also possibly use auto_ptr which is left empty if not used.
Example with shared_ptr:
Code:
template < typename T > struct no_op_deleter
{
void operator() ( T * ) {} // do not delete the pointer
};
shared_ptr< stream > uncompStream;
if( !compressed )
{
uncompStream.reset( &input, no_op_deleter< stream >() );
}
else
{
uncompStream.reset( new uncompress_stream( &input ) );
}
Example using auto_ptr
Code:
stream * uncompStream;
std::auto_ptr< stream > autoStream;
if ( !compressed )
{
uncompStream = &input;
}
else
{
autoStream.reset( new uncompress_stream( &input );
uncompStream = autoStream.get()
}
// rest of function
-
May 18th, 2009, 11:39 AM
#4
Re: Temporaries problem (C++)
-
May 18th, 2009, 11:45 AM
#5
Re: Temporaries problem (C++)
You'd be doing the same thing under the hood more or less, but this seems like a case where the Factory pattern could aid in maintaining the abstraction.
-
May 18th, 2009, 02:30 PM
#6
Re: Temporaries problem (C++)
It's an ugly problem, IMO.
The parameter input is clean enough - a reference, no problems there.
The problem comes in with the dynamically allocated de-compressor. In one context you have something to delete, but not in the other. That's the ugly part.
I'd personally prefer to choose one of a few other methods, and that's primarily out of habit to avoid situations like this, which if handed off to other developers becomes something they have to 'know'.
I'd call one of two functions, differentiated between one and the other paradigm, making this an inline.
Alternatively, I'd change the function so the parameter is a shared_ptr of the input stream, but that assumes I could make the de-compressor accept a smart pointer (which it just might if I made it). The shared_ptr also helps support threaded work, so I could toss this stream to other threads at will without further containment questions.
My point is that I suggest you establish habits that avoid this kind of "dual mode containment" question (probably the reason you sought inquiry in the first place, it's on your mind - and that's excellent).
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|