key9 said:
Hi all
I am confuse of how to override operate "<<"
Sample , I've got 2 class
class Terminal
{
public:
void printchar(string ch){
printf(ch); // don't care this
};
}
class Adapter
{
private:
void printWithPrefix(string str){
tm_ << prefix_ << str;
}
private:
string prefix_;
Terminal* tm_;
}
What I want to implement is
void foo(){
Terminal *term_ = new Terminal();
Adapter* adp_ = new Adapter(term_);
string str = "This is test string"
char ch = 'a'
adp_ << str << ch;
}
>
what's the right grammar?
The operator<< is usually implemented as global function (in contrast to
a method that belongs to a class). In your case you need to define some
overloadings of operator<< for different types (these are not called
overridings, as you did).
For example to output a std::string you have to define something like this:
#include <sstream>
class Terminal
{
public:
void printchar(std::string ch){
printf(ch.c_str ()); // don't care this
};
};
class Adapter
{
public:
// I had to insert this (rather dumb) constructor.
Adapter (Terminal* p_Terminal)
: tm_ (p_Terminal),
prefix_ ("TestPrefix")
{}
public:
// Adapters print method must be public, else we
// can't access its functionality.
void printWithPrefix (std::string str)
{
tm_->printchar (prefix_);
tm_->printchar (str);
}
private:
std::string prefix_;
Terminal* tm_;
};
// These are the overloadings of operator<< for Terminals. They
// are not really necessary for this example, but nice to have.
Terminal& operator<< (Terminal& p_Terminal, std::string p_String)
{
// Let the terminal print the string as it is.
std::stringstream Temp;
Temp << p_String;
p_Terminal.printchar (Temp.str ());
return p_Terminal;
}
Terminal& operator<< (Terminal& p_Terminal, int p_Number)
{
// Convert the number to a string and let the terminal print the
// text representation of the number.
std::stringstream Temp;
Temp << p_Number;
p_Terminal.printchar (Temp.str ());
return p_Terminal;
}
Adapter& operator<< (Adapter& p_Adapter, std::string p_String)
{
// Let the terminal print the string as it is.
std::stringstream Temp;
Temp << p_String;
p_Adapter.printWithPrefix (Temp.str ());
return p_Adapter;
}
Adapter& operator<< (Adapter& p_Adapter, int p_Number)
{
// Convert the number to a string and let the terminal print the
// text representation of the number.
std::stringstream Temp;
Temp << p_Number;
p_Adapter.printWithPrefix (Temp.str ());
return p_Adapter;
}
// Instead of the above two overloadings you can
// use the following template:
//template <class t_ParameterType>
//Adapter& operator<< (Adapter& p_Adapter, t_ParameterType p_Parameter)
//{
// // Convert the number to a string.
// std::stringstream Temp;
// Temp << p_Parameter;
// p_Adapter.printWithPrefix (Temp.str ());
// return p_Adapter;
//}
void foo()
{
Terminal *term_ = new Terminal();
Adapter adp_ (term_);
std::string str ("This is test string");
char ch = 'a';
(*term_) << str << ch;
adp_ << str;
}
int main ()
{
foo ();
return 0;
}
Now this source should be compilable (you should always post only such
programs that can actually be compiled, I had to correct numerous errors
in your source!)
Regards,
Stuart