| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"key9" |
| Date: |
12 Sep 2006 06:37:27 AM |
| Object: |
How I should write code then I can use operate "<<" |
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?
If I have an other class A, when I want to using
class A;
adp_<< A;
what's that grammar?
thank you very much
key9
.
|
|
| User: "Morten V Pedersen" |
|
| Title: Re: How I should write code then I can use operate "<<" |
12 Sep 2006 07:32:58 AM |
|
|
Hey Key9,
Here's a small example showing how the << operator can be overloaded:
class Foo
{
public:
void printFoo() const {
std::cout << foo_ << std::endl;
}
/** Implement this smarter, but you get the idea */
Foo& operator <<(const std::string &foo){
foo_ = foo;
return *this;
}
private:
std::string foo_;
};
Example:
Foo foo;
foo << "hello foo";
foo.printFoo();
Also check out:
http://www.parashift.com/c++-faq-lite/operator-overloading.html
// Morten
key9 wrote:
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?
If I have an other class A, when I want to using
class A;
adp_<< A;
what's that grammar?
thank you very much
key9
.
|
|
|
|
| User: "Stuart Redmann" |
|
| Title: Re: How I should write code then I can use operate "<<" |
12 Sep 2006 08:10:38 AM |
|
|
key9 wrote:
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
.
|
|
|
| User: "key9" |
|
| Title: Re: How I should write code then I can use operate "<<" |
12 Sep 2006 10:20:24 AM |
|
|
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!)
sorry for that ,and thank you very much
I am a computer fan ,but I am not a coder, so every time when I try to
design something , the problem always exist on grammar . It's my suffering.
thanks GOD there's kindness guys like you.
.
|
|
|
|
|

|
Related Articles |
|
|