|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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(); } }; |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|