How I should write code then I can use operate "<<"

K

key9

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
 
M

Morten V Pedersen

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
 
S

Stuart Redmann

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
 
K

key9

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top