Jump to content
  • Advertisement
Sign in to follow this  
cptrnet

operator overloading

This topic is 4845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.";
}




Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!