
-----------------------------------
The_$hit
Wed Apr 27, 2005 8:48 pm

Overloading Operator&lt;&lt;
-----------------------------------
Does any one know how to overload the operator > to use it for std::cout or std::cin? I got an understanding of how to do it but my compilier wont let me have 2 parameters when i overload operators.

right now it looks like this:

ostream& operator. there are no syntax errors but it would let me cout ) operators are not members of the class.  However they are friends, and this is important because that status allows them to access private and protected members of the class.

-----------------------------------
The_$hit
Thu Apr 28, 2005 4:59 pm


-----------------------------------
ooo ok. So how would you make it so that you can cin an object is this right


friend std::ostream& operator>>(std::ostream& out, String& temp){char * blah=new char[80];cin>>blah;for (int i=0;i>(std::istream& in, String& temp)
{
   char * blah = new char

Now, to go any further, I need to see your String class.  Let me guess though, it looks looks a bit like:

#include 
#include 

class String
{
   private:
      // store the characters
      char * buffer;
   public:
      // default constructor
      String();
      // copy constructor
      String(const String& other);
      // destructor
      ~String();

      // get a letter at a particular index
      char letter(size_t index) const;
      // get a reference to a letter at a particular position
      // changes to a letter can be made with something like:
      // some_string.letter(43) = 'A';
      char& letter(size_t index);

      friend std::ostream& operator(std::istream& out, String& s);
};

String::String() : buffer(new char

A final note for this post: why are you doing this?  Standard C++ provides a perfectly good string class in std::string.

-----------------------------------
The_$hit
Thu Apr 28, 2005 9:10 pm


-----------------------------------
I am doing this class so i can customize it and i know all of the functions in it. It is also for good for practice with overloading operators, arrays, i was also thinking that it would help me understand pointers.


#include 
using namespace std;

class String
{
public:
String(){itsString=new char[1];itsString[0]='\0';itsLength=1;}

String(String & temp){itsLength=temp.getLength();itsString=new 
char[itsLength];for(int x=0;x> ) operator.

class foo
{
   private:
      int a;
   public:
      int get_a() const;
      friend std::istream& operator>>(std::istream& in, foo& f);
};

int foo::get_a() const
{
   return a;
}

std::istream& operator>>(std::istream& in, foo& f)
{
   in >> f.a;
   return in;
}

So, what am I doing here?  Well, I have a class "foo" with a private instance variable "a" which is an int.  I have a public method "get_a()" to retrieve that value.  I also have a friend, which is the extaction operator.

The implementation of get_a() is so simple it warrants no further mention.

Now, when I implement the extraction operator, it isn't a member of the foo class.  Rather it takes the input stream and a reference to a foo object.  The operator reads into that object's "a" instance variable, and then the input stream is returned, allowing for chaining them together, like so:

foo b, c, d, e, f;
std::cin >> b >> c >> d >> e >> f;

Any questions?

-----------------------------------
The_$hit
Sat Apr 30, 2005 12:15 am


-----------------------------------
Thank you very much for all of your help. My completed String class is here:


//Created By: Julian Haldenby

#include 

using namespace std;

class String
{
public:

String(){
itsString=new char[1];
itsString[0]='\0';
itsLength=1;
}

String(String & temp){
itsLength=temp.getLength();
itsString=new char[itsLength];
for(int x=0;x