operator overloading

Started by
7 comments, last by cptrnet 18 years, 8 months ago
Hi Im trying to learn operator overloading. This code here is fine.

void operator = (char* str)
{
  m_str = str;
}

But this code errors out saying error in function declaration; skipping function body, does anyone know why. Im a retard when it comes to this. Thanks.

char* operator = (void)
{
  return m_str;
}

char* operator << (void)
{
  return m_str;
}

If you insist on saying "DUH", try to make a post that makes a little more sense
Advertisement
operator=() and operator<<() are binary operators, you have to have something on the right hand side in order to use the = sign or <<. By declaring operator=(void) you try to declare operator=() as a unary operator. I'm not really sure what you are trying to accomplish with those two functions so I can't say how to fix them.

char* str = Class;

OR

std::cout << Class;
If you insist on saying "DUH", try to make a post that makes a little more sense
You can't overload operators for primitive types. Check out this page (and specifically, this question) for more info.

Edit: Sorry, misunderstood what you were trying to do
how does the string class do it then

std::string str = "Text";

std::cout << str;
If you insist on saying "DUH", try to make a post that makes a little more sense
For the first one, you need to create a conversion operator. It looks like:

class Class {  public:    operator char *() { return str; }        char * str;};

That being said, providing conversion operator are generally a bad idea. Implicit conversions can bite you in the rear. Prefer to write a member function that returns a pointer instead.

For the second one, you need a non-member overload of operator<< since the object on the left hand side is a std::ostream. It looks like:
std::ostream & operator<<(std::ostream & lhs, Class & rhs) {  lhs << /* whatever */}

You may need to friend the function to get the insides to compile.
To overload the stream insertion operator (the << operator) you will need to declare it as:

ostream& operator<<(ostream&, ClassName);


Typically this declaration is used within a class, allowing the user to print out various class information / data.

Now when you define the overloaded operator function, its defined like this:

ostream& operator<<(ostream& osObject, ClassName& name){   osObject << "Whatever info you want printed out here.";}
Quote:Original post by cptrnet
std::string str = "Text";

This one is done by providing a constructor that takes a const char * argument, it has nothing to do with operator overloading. Though

std::string str;
str = "Text";

Is done by providing an overload of operator=() that takes a const char * argument like the first thing in your original post.
Cool thanks alot SiCrane you've helped out of so many jams, And thanks to the other posters I will take a look at the page about overloading.
If you insist on saying "DUH", try to make a post that makes a little more sense

This topic is closed to new replies.

Advertisement