开发者

structure versus classes

开发者 https://www.devze.com 2023-04-02 15:09 出处:网络
// By using structure : struct complex { float real; float imag; }; complex operator+(complex, complex); main() {
// 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 overlo开发者_开发问答aded 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?


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


...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).


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.


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.


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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消