4
// By using structure :     
struct complex {
  float real;
  float imag;
};    

complex operator+(complex, complex);    

main() { 
  complex t1, t2, t3;    
  t3 = t1 + t2;    
}    

complex operator+(complex w, complex z) {
  statement 1;    
  statement 2;   
}    

// By using class :    
class complex {
  int real;
  int imag;    

public:    
  complex operator+(complex c) {
    statement 1;    
    statement 2;    
  }    

  main() {    
    complex t1, t2, t3;    
    t3 = t1 + t2;    
  }    

While using structure, the overloaded function can accept two arguments whereas while using class the overloaded function accepts only one argument, when the overloaded operator function is a member function in both cases i.e in struct as well as in class. Why does this happen?

shubhendu mahajan
  • 816
  • 1
  • 7
  • 16
  • 1
    format your code before asking the question please. – Mu Qiao Sep 06 '11 at 14:40
  • You should format code by putting four spaces before each line. You can also select it and click the `{}` button. More helpful tips at the [Markdown Editing Help](http://stackoverflow.com/editing-help) page. – R. Martinho Fernandes Sep 06 '11 at 14:40
  • please fix the formatting and formulate a question. You should also know that you can define methods on a struct, which would essentially result in the exact equivalent of your `complex` class. – Niklas B. Sep 06 '11 at 14:41
  • 3
    Having fixed the formatting, I've determined there is no good question here. – i_am_jorf Sep 06 '11 at 14:46
  • 1
    @Mat: Why did you edit out what was probably the main reason for the question? The OP apparently believed that the operator is a *member* is the first case as well. It is not a member, of course, but by editing it out you completely changed the question. – AnT stands with Russia Sep 06 '11 at 14:46
  • @AndreyT: I have no recollection of removing that although I see it in the history... I think my edit probably munged desprado07's one (being "more substantial"), I hadn't seen that text at all when I read the question. (It certainly took me more than 7 seconds to attempt that formatting fix) – Mat Sep 06 '11 at 14:50
  • @Mat, AndreyT: I suppose that if desprado07 bothered to look at the preview before posting the question, none of this confusion would've happened... – Armen Tsirunyan Sep 06 '11 at 15:00

6 Answers6

10

That has nothing to do with classes vs. structs. It's about member vs. nonmember.

Classes and structs in C++ differ solely bu their default accessibility level for members and bases (public for structs, and private for classes). Other than this, there is no difference at all.

When overloading operators, you almost always have the choice of defining an operator as a member or as a freestanding function. There are only 4 operators that have to be members. These are: (), [], ->, and = (as to why, see this question of mine). For the rest, the choice is yours.

This excellent FAQ entry explains (among other things) how to choose between member vs. nonmember.

To answer your core question: In case of member function, the first agument is *this

Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
3

...when the overloaded operator function is a member function in both cases i.e structure as well as class...

What makes you say that? That's not true.

In case of struct in your example, overloaded operator function is not a member. This is why it requires 2 parameters.

So, the difference has absolutely nothing to do with struct vs. class matter. The reason you have different number of parameters in these operators is that the first one is implemented as a non-member (and therefore has two explicit parameters), while the second one is implemented as member (and therefore has only one explicit parameter).

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

This happens because you using different methods to define operator. When you use struct you define it outside of struct, but when use class you define operator inside class.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
Eugene Burtsev
  • 1,465
  • 4
  • 24
  • 45
0

When you want to pass two args to your overloaded operator+ you should create a friend function of the class and then pass const complex& lhs, const complex& rhs as your args. Now you can have two arguments to your operator+.

When the operator+ is a member of the class, one arg is implicitly passed as the this pointer.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
-1
//
complex operator +(cont complex& lhs, const complex& rhs)
{
  ...
}

main()  
{ 
  complex t1,t2,t3;    
  t3=t1+t2; // i.e. t3 = operator+(t1, t2);
}

// 
class complex
{ 
  int real,imag;    
public:    
  complex operator +(const complex& rhs){}
};

main()    
{    
  complex t1,t2,t3;    
  t3=t1+t2; // i.e. t3 = t1.operator+(t2);
}    
user928204
  • 230
  • 2
  • 6
-2

In your second example the overloaded operator is a member function of the class, therefore when this operator is called, it's called as a member function of the first operand and the first operand is therefore implicitly given as the object on which the member function has been called (the this pointer if you like).

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
  • While correct, that's a run-on sentence I had to read several times to see if it was right. – Mooing Duck Sep 06 '11 at 16:27
  • @Mooing And you came to the result that it's wrong? Or was it only a grammar-related downvote? While the accumulation of main clauses may be seen as bad style, the deduction process of its content is perfectly linear. If you are not the downvoter, then excuse me for blaming you. – Christian Rau Sep 06 '11 at 17:35
  • As I said, the result is correct. My complaint is merely grammar-related. – Mooing Duck Sep 06 '11 at 17:43