473,395 Members | 1,978 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,395 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 2140
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() {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.