结构与类

发布于 2024-12-03 08:05:22 字数 624 浏览 1 评论 0原文

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

使用结构时,重载函数可以接受两个参数,而使用类时,重载函数仅接受一个参数,当重载运算符函数在两种情况下(即在结构体和类中)都是成员函数时。为什么会出现这种情况?

// 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

千と千尋 2024-12-10 08:05:23

当您想将两个参数传递给重载的operator+时,您应该创建该类的友元函数,然后传递constcomplex& lhs,const 复杂& rhs 作为你的参数。现在,您的 operator+ 可以有两个参数。

operator+ 是类的成员时,一个arg 会作为this 指针隐式传递。

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.

月亮是我掰弯的 2024-12-10 08:05:23
//
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);
}    
//
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);
}    
粉红×色少女 2024-12-10 08:05:23

在第二个示例中,重载运算符是类的成员函数,因此当调用此运算符时,它被作为第一个操作数的成员函数调用,因此第一个操作数隐式给出为成员函数所在的对象调用(如果您愿意,可以使用 this 指针)。

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

日久见人心 2024-12-10 08:05:22

这与类与结构无关。这是关于会员与非会员的。

C++ 中的类和结构体的不同之处仅在于其成员和基类的默认可访问性级别(结构体为公共,类为私有)。除此之外,根本没有任何区别。

当重载运算符时,您几乎总是可以选择将运算符定义为成员或独立函数。只有 4 名操作员必须是会员。它们是:()[]->=(至于为什么,请参见< a href="https://stackoverflow.com/questions/3938036/rationale-of-enforcing-some-operators-to-be-members">我的这个问题)。对于其余的,选择权在你。

这个优秀的常见问题解答条目解释了(除其他外)如何在会员与非会员之间进行选择。

回答您的核心问题:对于成员函数,第一个参数是 *this

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

时光瘦了 2024-12-10 08:05:22

...当重载运算符函数是两者中的成员函数时
案例,即结构以及类...

你为什么这么说?那不是真的。

对于示例中的struct,重载运算符函数不是成员。这就是为什么它需要 2 个参数。

因此,这种差异与结构与类的问题完全无关。这些运算符中参数数量不同的原因是第一个运算符作为非成员实现(因此具有两个显式参数),而第二个运算符作为成员实现(因此只有一个显式参数)。

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

落叶缤纷 2024-12-10 08:05:22

发生这种情况是因为您使用不同的方法来定义运算符。当您使用结构时,您在结构之外定义它,但是当使用类时,您在类内部定义运算符。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文