A
Adam Nielsen
Hi everyone,
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.
I'm not sure whether the object is being constructed correctly, but as I
can't call the str() function I'm guessing not.
Could someone please have a look at the example code below and let me
know what I'm doing wrong?
Thanks,
Adam.
#include <iostream>
#include <sstream>
struct PrintString
{
PrintString(std::string s)
{
std::cout << s << std::endl;
}
};
struct PrintNumber: public PrintString
{
PrintNumber(int iNumber)
: PrintString(
// How should this be done?
(std:stringstream() << "The number is " << iNumber).str()
)
{
}
};
int main(void)
{
PrintNumber(5);
return 0;
}
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.
I'm not sure whether the object is being constructed correctly, but as I
can't call the str() function I'm guessing not.
Could someone please have a look at the example code below and let me
know what I'm doing wrong?
Thanks,
Adam.
#include <iostream>
#include <sstream>
struct PrintString
{
PrintString(std::string s)
{
std::cout << s << std::endl;
}
};
struct PrintNumber: public PrintString
{
PrintNumber(int iNumber)
: PrintString(
// How should this be done?
(std:stringstream() << "The number is " << iNumber).str()
)
{
}
};
int main(void)
{
PrintNumber(5);
return 0;
}