473,221 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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


Sep 12 '06 #1
3 2133
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-lit...erloading.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

Sep 12 '06 #2
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
Sep 12 '06 #3
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.
Sep 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Eric A. Forgy | last post by:
Hello, I am just learning Java and am trying to write a method that does something like //=========================================== public Static List find(double array,double val,String...
7
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex'...
1
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
6
by: Rate Spring | last post by:
hi, in win2000 and VC6, I operate a txt file. When I write a line data into file and add "\r\n" at the end of line, I find: fstream << "\r\n" write 3 chars : 0D 0D 0A fstream << "\n" ...
2
by: bissatch | last post by:
Hi, I am trying to use JavaScript to write a table column on a web page. The code is as follows: <html> <head> <script> function displaycount() {
4
by: wangzhihuii | last post by:
Hi all, I'm really confused, can cout<<""; contribute anything to the routine ?!! my programm won't work properly without this trivial sentence. Sincerely vivian
12
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
3
by: Steven.Xu | last post by:
hi everybody, i am useing some classes which in System.Xml to deal with xml document. what can i do if the document include "<" or ">"? Thanks.
4
by: ek | last post by:
I thought that the syntax for c++ in either winXP or linux was the same. In Ubuntu linux it works fine if I write: #include <string> using namespace std; int main() {
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.