Jump to content
  • Advertisement
Sign in to follow this  
dbzprogrammer

Overloading

This topic is 4265 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

Wow, I thought it might make my code cleaner... In certain parts yes, in the actual overlosion, NO! Anyways, I'm just wondering HOW I should code it. It's very confusing about how it all works, so I've avoided it mostly. But, I need to learn it know. So, my first question is, does anyone have any good tutorials on overloading? I mean ones that EXPLAIN why you phrase it this way: void operator<<(int& a); And not this way: friend Stream& operator(Stream& stream, int& a); I've got to be honest, I know what friend are, but the whole overloading scheme doesn't make much sense... Anyways, onto my next question: I have a stream class. It is basically a higher level file class. It saves different kinds of string I use in my program. I don't use inheritance. Anyways, here's a bit of code from it:
friend Logger& operator<<(Logger&,Exception&);
//Later in the code
logger << exception;
Yuck! Could I do it this way?
void operator<<(Exception&);
//Later in the code
logger << exception;
Anything to point me in the right direction is more than welcome!

Share this post


Link to post
Share on other sites
Advertisement
You're talking about operator overloading, not just "overloading" (the latter is a generalization of the former).

Quote:

Anyways, I'm just wondering HOW I should code it.

Implement operator overloads that make sense for their context and do not alter the underlying semantics of the operator. Preserve expected behaviors (such as that of prefix or postfix increment). Read this.

Quote:

I mean ones that EXPLAIN why you phrase it this way:

void operator<<(int& a);

And not this way:

friend Stream& operator(Stream& stream, int& a);

I've got to be honest, I know what friend are, but the whole overloading scheme doesn't make much sense...


friend is not strictly related here. Both forms of the overload are valid (assuming the first is a member function). For those operators that can be defined as members or non-members (some have restrictions), the choice between member and non-member is usually based on your judgement. There are situations where it is beneficial to do one or the other, however. For example, overloading operator* between a (geometric) vector and a float requires two overloads: one for operator*(float,vector) and one for operator*(vector,float) since either form is valid. In this case, the programmer will usually elect to implement the operators as non-members since you cannot provide the first form as a member, and one would generally prefer to the two forms of the operator to appear near eachother in the header.

Quote:

Yuck! Could I do it this way?


I don't see what is so "yucky" about the non-member version here, or how your member version is any prettier. It removes one (explicit) parameter.

Also note that the "friend" declaration is not required, and that your member version violates the "expected behavior" tenet, above (it does not return the Logger by reference and thus you cannot chain the operator).

Share this post


Link to post
Share on other sites
Quote:
Original post by dbzprogrammer
So, my first question is, does anyone have any good tutorials on overloading? I mean ones that EXPLAIN why you phrase it this way:

void operator<<(int& a);

And not this way:

friend Stream& operator(Stream& stream, int& a);

Because the latter way would require you to write code like this.

Stream ostr;
int.operator<<(ostr);

Which isn't valid code.
Quote:

Anyways, onto my next question:

I have a stream class. It is basically a higher level file class. It saves different kinds of string I use in my program. I don't use inheritance. Anyways, here's a bit of code from it:
friend Logger& operator<<(Logger&,Exception&);
//Later in the code
logger << exception;
Yuck! Could I do it this way?
void operator<<(Exception&);
//Later in the code
logger << exception;


Mmm, hows about this?

class Exception
{
// ... blah blah blah ...
void writeTo(Logger& logger);
};

Logger& operator<<(Logger& logger, const Exception& ex)
{
ex.writeTo(logger);
return logger;
}

// ... more blather ...

logger << exception1 << '\n' << exception2 << '\n';


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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!