auto_ptr and cast

24 views
Skip to first unread message

Laurent Guéguen

unread,
Sep 27, 2012, 6:26:57 AM9/27/12
to Bio++ Development Forum
Hi,

in bpp-core OutputStream.h, I added a class to use std::stringstream,
because I needed
a object that knows how to convert the stream in a string. So I added
this code

#include <sstream>
class StdStr :
public StlOutputStreamWrapper
{
public:
StdStr(): StlOutputStreamWrapper(new std::ostringstream()){}

std::string str() const { return dynamic_cast<const
std::ostringstream*>(stream_)->str();}

~StdStr() { delete stream_;}
};

It works fine, but I could not use the inheritance from
StlOutputStream in this, because there the stream_ is an
auto_ptr<std::ostream> , and I need something like dynamic_cast and I
could not manage this.

Cheers,
Laurent

Julien Yann Dutheil

unread,
Sep 27, 2012, 7:28:19 AM9/27/12
to biopp-de...@googlegroups.com
Hi Laurent,

I am not sure I get your point... ostringstream is a ostream, so you
should be able to use StlOutputStream with it.
Then you should be able to use
dynamic_cast<ostringstream*>(stream_->get()), isn't it?

Cheers,

Julien.
> --
> You received this message because you are subscribed to the Google Groups "Bio++ Development Forum" group.
> To post to this group, send email to biopp-de...@googlegroups.com.
> To unsubscribe from this group, send email to biopp-devel-fo...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Julien Y. Dutheil, Ph-D
0 (+49) 6421 178 530

§ Max Planck Institute for Terrestrial Microbiology
Department of Organismic Interactions
Marburg -- GERMANY

§ Intitute of Evolutionary Sciences - Montpellier
University of Montpellier 2 -- FRANCE

Laurent Guéguen

unread,
Sep 28, 2012, 3:50:31 AM9/28/12
to Bio++ Development Forum
For this I agree, but the problem is for the constructor, he needs
an auto_ptr<ostream> which would be the result of a cast
from an auto_ptr<ostringstream>. But I did not find the way to do it

L
> > For more options, visithttps://groups.google.com/groups/opt_out.

Julien Yann Dutheil

unread,
Sep 28, 2012, 5:29:37 AM9/28/12
to biopp-de...@googlegroups.com
Indeed,

Actually giving an auto_ptr to the constructor might not be a good
idea, I should check that.
Otherwise you should be able to do:
auto_ptr<ostringstream> ptr1 ...
auto_ptr<ostream> ptr2(ptr1.get());
ptr1.release();
and then give ptr2 to the constructor?

J.
> For more options, visit https://groups.google.com/groups/opt_out.

Laurent Guéguen

unread,
Oct 8, 2012, 4:58:19 AM10/8/12
to biopp-de...@googlegroups.com
Perhaps,  but I do not know how to put all this in one constructor line, since StlOutputStream needs it for its constructor.

I maintain pointer usage up to now.

Cheers,
L
Reply all
Reply to author
Forward
0 new messages