operator overloading

I have some questions with no better place to get answers from here?

1-why static functions aren't used with operator overloading despite they are working with objects or without?

2-member functions for operators aren't called implicitly if the left operand isn't object of this class, can it be called explicitly?

3-global functions provide commutative services for operators overloaded, HOW?

4-Why returning a reference from a function helps in cascading?

5-In this definintion
ClassName &ClassName::functionName(param)
return *this;
why doubling the ClassName when using *this return?

6-Can the copy constructor be used with normal array instead of dynamic allocated array?

7-What is the difference between (a=b=c) and (a=b)=c and why the const reference return prevents the second form?

sorry for all those questions
These sound like test/homework questions.

EDIT:

Also this drives home why I hate school. Look at question number 2:

2-member functions for operators aren't called implicitly if the left operand isn't object of this class, can it be called explicitly?


Talk about a useless question. They always have to waste people's time and make them memorize oddly-specific jargon that doesn't matter at all in actual application.

bah @ schools
Last edited on
it sounds like home work questions really
but i was reading in Pearson how to program book and those are my own notes not a home work and i hope to get answers for them?
especially cascading question
Last edited on
+1 Disch, I can't even read some of this garbage I really hope this is an instance of Google Translator being too literal.

Also regardless of the answers the instructor wants to hear, I guarentee that with about a years worth of time screwing around with C++ you can prove EVERYONE of their assumptions wrong.

I personally love number 3:
3-global functions provide commutative services for operators overloaded, HOW?

Now to me, this is the logical equivalent of asking "Water is wet. Why?" I can give you an answer, and it would be a correct one. But you won't like it.
Though grammatically incorrect, those are actual words used in jargon. And I disagree with jargon being useless, there are usually no words for those concepts in general language, so everytime you talked about it you would have to use some roundabout description to tell people what you are actually talking about. Though you are right, those sound like typical school questions.

Ok, if you really need answers for those...

why static functions aren't used with operator overloading despite they are working with objects or without?

because static functions exist in the namespace of the class. You would have to write something like class1::operator+(object1,object2) if you declared operators as static functions.

member functions for operators aren't called implicitly if the left operand isn't object of this class, can it be called explicitly?

You can call operators explicitly with object1.operator+(args), but this isn't really achieving anything, is it?


global functions provide commutative services for operators overloaded, HOW?

Because unlike member operators, with global operators you have control over both, the left hand argument and the right hand argument. Member operators can only take a left hand argument. This is only a problem if the left hand and right hand arguments can have different types though.

Why returning a reference from a function helps in cascading?[/quotes]
Cascading is for example, a = b = c. Here the return value of b=c (which is probably a reference to b) can be assigned to a. It doesn't really make any difference whether you return a reference or a copy though, except maybe that the efficiency on runtime may vary (there are cases in which returning a reference is faster, and there are cases in which returning a copy is faster).

[quote]In this definintion
ClassName &ClassName::functionName(param)
return *this;
why doubling the ClassName when using *this return?


This means: The function functionName that belongs to the class ClassName returns a reference to a ClassName instance.

Can the copy constructor be used with normal array instead of dynamic allocated array?
I am sorry, but I do not quite understand this question.

What is the difference between (a=b=c) and (a=b)=c and why the const reference return prevents the second form?

a=b=c first assigns c to b, and then assigns a to b. (a=b)=c first assigns b to a, and then c to a. If the = operator returns a constant reference this is not possible, because you can't assign anything to a constant.



Last edited on
Teachers don't use them in a sensible way though. I can't even tell you how many I've had that just throw terms around or use them in such odd ways that they are borderline wrong just to confuse you. It isn't about teaching at that point it's intellectual masturbation.

@ OP: If these really are your questions then I appoligies for the comment about your grammer. For you question about "cascading" I don't recognize that term, is this what you mean?

1
2
3
4
5
6
7
8
9
int IsFive(int intArg)
{
   if(intArg <= 5)
      {return intArg;}

  else
     {intArg--;
       return IsFive(intArg);}
}
Last edited on
sorry for my grammar
what i am asking about cascading , that i want to understand the concept it self for ex.

cin>>x>>y>>z;
if the stream extraction operator is overloaded, and the overloading function returns a reference , how would the compiler process it.
cin>>x>>y>>z;

Is the same thing as
cin>>x; cin>>y; cin>>z;

Because cin.operator>> returns a reference to cin.
Topic archived. No new replies allowed.