Afficher un message
Vieux 17/10/2007, 00h03   #1
Old Wolf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How do you create and use an ostringstream in an initialisation list?

On Oct 12, 3:22 pm, Adam Nielsen <adam.niel...@remove.this.uq.edu.au>
wrote:
> I'm trying to work out how to construct a temporary object in a class'
> initialisation list and then call a function on it, in order to pass the
> result to a base class' constructor.
> struct PrintNumber: public PrintString
> {
> PrintNumber(int iNumber)
> : PrintString(
> // How should this be done?
> (std:stringstream() << "The number is " << iNumber).str()
> )
> {
> }
> };


I would write:

struct PrintNumber: public PrintString
{
PrintNumber(int iNumber) : PrintString( convert_number(iNumber) ) {}

private:
std::string convert_number(int n)
{
std:stringstream oss;
oss << "The number is " << n;
return oss.str();
}
};

  Réponse avec citation
 
Page generated in 0,05673 seconds with 9 queries