📜  重载逗号运算符

📅  最后修改于: 2021-06-01 00:51:31             🧑  作者: Mango

在C++中,我们可以使用Operator Overloading重载逗号运算符例如:对于“将查询X发送到服务器Y并将结果放入变量Z”“和”起逗号的作用。逗号运算符(,)用于隔离两个或多个仅包含一个表达式的表达式。当必须为操作数求解表达式集时,仅考虑最右边的表达式。

例子:

抵抗过载的操作员如下:

  • 范围解析运算符(::)
  • sizeof()
  • 成员选择器(。)
  • 成员指针选择器(*)
  • 三元运算符(?:)

句法:

运算符重载规则:

  • 现有运算符只能重载,而新运算符不能重载。
  • 重载的运算符包含至少一个用户定义的数据类型的操作数。
  • 朋友函数不能用于重载某些运算符。但是,成员函数可用于重载那些运算符。
  • 当一元运算运算符通过成员函数重载时,不带任何显式参数,但是,如果它们由友元函数重载,则带一个参数。
  • 当二进制运算符通过成员函数重载时,将使用一个显式参数,如果它们通过朋友函数被重载,则将使用两个显式参数。

在下面的代码中,尽管每个表达式都是由编译器求值的,但左侧表达式的值却被丢弃了。最后,右手操作的值由函数返回。这将触发重载的逗号运算符,使其函数类似于其默认操作。

CPP
// C++ program to illustrate the
// overloading the comma operator
#include 
using namespace std;
  
// Comma class
class comma {
    int x, y;
  
public:
    // Default Constructor
    comma() {}
  
    // Parameterized Constructor
    comma(int X, int Y)
    {
        x = X;
        y = Y;
    }
  
    // Function to display the value
    void display()
    {
        cout << "x=" << x << " ";
        cout << "y=" << y << " ";
    }
  
    comma operator+(comma ob2);
    comma operator, (comma ob2);
};
  
// Function to overload the + operator
comma comma::operator+(comma ob2)
{
    comma temp;
  
    // Update the value temp
    temp.x = ob2.x + x;
    temp.y = ob2.y + y;
  
    // Return the value temp
    return temp;
}
  
// Function to overload the, operator
comma comma::operator, (comma ob2)
{
    comma temp;
  
    // Update the value temp
    temp.x = ob2.x;
    temp.y = ob2.y;
  
    // Print the value
    cout << "x=" << ob2.x << " "
         << "y=" << ob2.y << endl;
  
    // Return the value temp
    return temp;
}
  
// Driver code
int main()
{
    // Initialize objects
    comma ob1(10, 20), ob2(5, 30), ob3(1, 1);
  
    ob1.display();
    ob2.display();
    ob3.display();
  
    cout << endl;
  
    ob1 = (ob2 + ob2, ob1, ob3);
  
    // Displays the value of
    // ob3 (Rightmost expression)
    ob1.display();
  
    return 0;
}


C++
// C++ program to illustrate the
// overloading for comma operator
  
#include 
using namespace std;
  
// The class that defines the
// coordinates of a point in space
class Coords3D {
  
private:
    double x, y, z;
  
public:
    // Default Constructor
    Coords3D() { x = y = z = 0; }
  
    // Parameterized Constructor
    Coords3D(double x, double y,
             double z)
    {
        this->x = x;
        this->y = y;
        this->z = z;
    }
  
    // Function for updating the value
    // ofx, y, and z
    void Get(double& x, double& y,
             double& z)
    {
        x = this->x;
        y = this->y;
        z = this->z;
    }
  
    // Function to overloaded the
    // operator, (comma)
    Coords3D operator, (Coords3D obj)
    {
        Coords3D tmp;
  
        // Update the value of temp
        tmp.x = obj.x;
        tmp.y = obj.y;
        tmp.z = obj.z;
  
        // Return the value of temp
        return tmp;
    }
};
  
// Driver Code
int main()
{
    double x, y, z;
  
    // Instances of class Coords3D
    Coords3D c1(1, 3, 5);
    Coords3D c2(2, 4, 6);
    Coords3D c3;
  
    // Invoke the operator function
    // c3.operator, (c2) into c3
    // is saved c2
    c3 = (c1, c2);
  
    // Get the value of x, y, z
    c3.Get(x, y, z);
  
    // Print x, y, and z
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "z = " << z << endl;
  
    // Create another instance
    Coords3D c4(10, 15, 20);
  
    // c3 <= c4
    c3 = (c2, c1, c4);
  
    // Checking
    // x = 10, y = 15, z = 20
    c3.Get(x, y, z);
  
    cout << endl;
  
    // Print x, y, and z
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "z = " << z << endl;
  
    return 0;
}


输出:
x=10 y=20 x=5 y=30 x=1 y=1 
x=10 y=20
x=1 y=1
x=1 y=1

下面是另一个例子 逗号(,)运算符在名为Coords3D的类中重载

  • 该类具有3个内部隐藏变量x,y,z
  • Get()方法,它是获取x,y,z值的访问方法。
  • 运算符函数运算符()会使运算符‘,’重载

以下是相同的程序:

C++

// C++ program to illustrate the
// overloading for comma operator
  
#include 
using namespace std;
  
// The class that defines the
// coordinates of a point in space
class Coords3D {
  
private:
    double x, y, z;
  
public:
    // Default Constructor
    Coords3D() { x = y = z = 0; }
  
    // Parameterized Constructor
    Coords3D(double x, double y,
             double z)
    {
        this->x = x;
        this->y = y;
        this->z = z;
    }
  
    // Function for updating the value
    // ofx, y, and z
    void Get(double& x, double& y,
             double& z)
    {
        x = this->x;
        y = this->y;
        z = this->z;
    }
  
    // Function to overloaded the
    // operator, (comma)
    Coords3D operator, (Coords3D obj)
    {
        Coords3D tmp;
  
        // Update the value of temp
        tmp.x = obj.x;
        tmp.y = obj.y;
        tmp.z = obj.z;
  
        // Return the value of temp
        return tmp;
    }
};
  
// Driver Code
int main()
{
    double x, y, z;
  
    // Instances of class Coords3D
    Coords3D c1(1, 3, 5);
    Coords3D c2(2, 4, 6);
    Coords3D c3;
  
    // Invoke the operator function
    // c3.operator, (c2) into c3
    // is saved c2
    c3 = (c1, c2);
  
    // Get the value of x, y, z
    c3.Get(x, y, z);
  
    // Print x, y, and z
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "z = " << z << endl;
  
    // Create another instance
    Coords3D c4(10, 15, 20);
  
    // c3 <= c4
    c3 = (c2, c1, c4);
  
    // Checking
    // x = 10, y = 15, z = 20
    c3.Get(x, y, z);
  
    cout << endl;
  
    // Print x, y, and z
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "z = " << z << endl;
  
    return 0;
}
输出:
x = 2
y = 4
z = 6

x = 10
y = 15
z = 20
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”