I just wrote the following and have a question: Why does my code gets
it wrong with the class Simple? See last show_size<Simpl e> function call
in main () as below:
#include <iostream>
#include <string>
#include <limits>
class Simple
{
public:
Simple();
~Simple();
void set(std::string text);
std::string get(void);
friend std::ostream& operator<<(std: :ostream& ostream, Simple&
simple);
private:
std::string t;
};
Simple::Simple( ) :
t("")
{
}
Simple::~Simple ()
{
}
void Simple::set(std ::string text)
{
t = text;
}
std::string Simple::get(voi d)
{
return t;
}
template <typename T>
const int get_n_bits()
{
return std::numeric_li mits<T>::digits + 1;
}
template <typename T>
const char* pluralise(const char* singluar, const char* plural)
{
return (sizeof(T) == 1) ? singluar : plural;
}
template <typename T>
std::ostream& show_size(std:: ostream& stream, const char* type)
{
stream << type << " has ";
stream << get_n_bits<T>() << " bits";
stream << ", has length of ";
stream << sizeof(T);
stream << " ";
stream << pluralise<T>("b yte", "bytes");
stream << ".\n";
return stream;
}
int main(int argc, char *argv[])
{
show_size<bool> (std::cout, "bool");
show_size<char> (std::cout, "char");
show_size<short int>(std::cout, "short int");
show_size<int>( std::cout, "int");
show_size<long int>(std::cout, "long int");
show_size<long long>(std::cout , "long long");
show_size<float >(std::cout, "float");
show_size<doubl e>(std::cout, "double");
show_size<long double>(std::co ut, "long double");
show_size<wchar _t>(std::cout, "wchar_t");
show_size<Simpl e>(std::cout, "Simple");
return 0;
}
--
http://www.munted.org.uk
Take a nap, it saves lives.