PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Re: How do you create and use an ostringstream in an initialisation list?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: How do you create and use an ostringstream in an initialisation list?

Réponse
 
LinkBack Outils de la discussion
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
Vieux 17/10/2007, 08h09   #2
James Kanze
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 17, 1:03 am, Old Wolf <oldw...@inspire.net.nz> wrote:
> 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)


I'm not sure that it's absolutely necessary, but I'd make this
function static. If the constructors that use it are all in a
single file, I'd put it in an anonymous namespace in the file,
rather than making it a member. And in the very particular case
here, there is some argument for making it a static protected
template member of the base class (but that's not usually
applicable).

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h42.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,07512 seconds with 10 queries