c+中的param和params用法+; 我正在学习C++的课程,我遇到了代码的片段,我想知道PARAM是用什么来做的。 // vectors: overloading operators example #include <iostream> using namespace std; class CVector { public: int x,y; CVector () {}; CVector (int,int); CVector operator + (CVector); }; CVector::CVector (int a, int b) { x = a; y = b; } CVector CVector::operator+ (CVector param) { CVector temp; temp.x = x + param.x; temp.y = y + param.y; return (temp); } int main () { CVector a (3,1); CVector b (1,2); CVector c; c = a + b; cout << c.x << "," << c.y; return 0; } //向量:重载运算符示例 #包括 使用名称空间std; 类CVector{ 公众: int x,y; CVector(){}; CVector(int,int); CVector运算符+(CVector); }; CVector::CVector(intA,intB){ x=a; y=b; } CVector CVector::运算符+(CVector参数){ CVector温度; 温度x=x+参数x; 温度y=y+参数y; 返回(临时); } int main(){ CVector a(3,1); CVector b(1,2); CVector c; c=a+b; cout

c+中的param和params用法+; 我正在学习C++的课程,我遇到了代码的片段,我想知道PARAM是用什么来做的。 // vectors: overloading operators example #include <iostream> using namespace std; class CVector { public: int x,y; CVector () {}; CVector (int,int); CVector operator + (CVector); }; CVector::CVector (int a, int b) { x = a; y = b; } CVector CVector::operator+ (CVector param) { CVector temp; temp.x = x + param.x; temp.y = y + param.y; return (temp); } int main () { CVector a (3,1); CVector b (1,2); CVector c; c = a + b; cout << c.x << "," << c.y; return 0; } //向量:重载运算符示例 #包括 使用名称空间std; 类CVector{ 公众: int x,y; CVector(){}; CVector(int,int); CVector运算符+(CVector); }; CVector::CVector(intA,intB){ x=a; y=b; } CVector CVector::运算符+(CVector参数){ CVector温度; 温度x=x+参数x; 温度y=y+参数y; 返回(临时); } int main(){ CVector a(3,1); CVector b(1,2); CVector c; c=a+b; cout,c++,C++,Param是+运算符的右操作数 以下是代码的更清晰版本: #include <iostream> using namespace std; class CVector { public: int x,y; CVector (); CVector (int,int); CVector (const CVector&); CVector operator + (const CVector&) const; }; CVecto

Param是
+
运算符的右操作数

以下是代码的更清晰版本:

#include <iostream>
using namespace std;

class CVector {
  public:
    int x,y;
    CVector ();
    CVector (int,int);
    CVector (const CVector&);
    CVector operator + (const CVector&) const;
};

CVector::CVector() : x(0), y(0) {}
CVector::CVector(int a, int b) : x(a), y(b) {}
CVector::CVector(const CVector& v) : x(v.x), y(v.y) {}

CVector CVector::operator+ (const CVector& param) const {
  CVector temp(*this);
  temp.x += param.x;
  temp.y += param.y;
  return (temp);
}

int main () {
  CVector a (3,1);
  CVector b (1,2);
  CVector c;
  c = a + b;
  cout << c.x << "," << c.y;
  return 0;
}
#包括
使用名称空间std;
类CVector{
公众:
int x,y;
CVector();
CVector(int,int);
CVector(const CVector&);
CVector运算符+(const CVector&)const;
};
CVector::CVector():x(0),y(0){
CVector::CVector(inta,intb):x(a),y(b){
CVector::CVector(constCvector&v):x(v.x),y(v.y){
CVector CVector::运算符+(常量CVector和参数)常量{
CVector温度(*此);
温度x+=参数x;
温度y+=参数y;
返回(临时);
}
int main(){
CVector a(3,1);
CVector b(1,2);
CVector c;
c=a+b;

coutParam是
+
运算符的右操作数

以下是代码的更清晰版本:

#include <iostream>
using namespace std;

class CVector {
  public:
    int x,y;
    CVector ();
    CVector (int,int);
    CVector (const CVector&);
    CVector operator + (const CVector&) const;
};

CVector::CVector() : x(0), y(0) {}
CVector::CVector(int a, int b) : x(a), y(b) {}
CVector::CVector(const CVector& v) : x(v.x), y(v.y) {}

CVector CVector::operator+ (const CVector& param) const {
  CVector temp(*this);
  temp.x += param.x;
  temp.y += param.y;
  return (temp);
}

int main () {
  CVector a (3,1);
  CVector b (1,2);
  CVector c;
  c = a + b;
  cout << c.x << "," << c.y;
  return 0;
}
#包括
使用名称空间std;
类CVector{
公众:
int x,y;
CVector();
CVector(int,int);
CVector(const CVector&);
CVector运算符+(const CVector&)const;
};
CVector::CVector():x(0),y(0){
CVector::CVector(inta,intb):x(a),y(b){
CVector::CVector(constCvector&v):x(v.x),y(v.y){
CVector CVector::运算符+(常量CVector和参数)常量{
CVector温度(*此);
温度x+=参数x;
温度y+=参数y;
返回(临时);
}
int main(){
CVector a(3,1);
CVector b(1,2);
CVector c;
c=a+b;

cout
Param
只是一个标识符,用于指定重载+运算符的右操作数。您可以使用任何其他标识符来代替它

CVector a, b;
a+b; //a is the object on which you call +, b is the param

Param
只是一个标识符,用于指定重载+运算符的右操作数。您可以使用任何其他标识符来代替它

CVector a, b;
a+b; //a is the object on which you call +, b is the param

重载中不需要参数,因为它是一个成员函数。仅当运算符+是非成员函数时才使用它。@DumbCoder运算符
++
有两个操作数,而不是一个!@Let_Me____Be-我相信它是一元+运算符。如果一元运算符是类成员,则不需要参数。@DumbCoder你是怎么做到这一点的结论:-D@DumbCoder:这算什么一元运算符?它显然在两个操作数之间执行加法运算。(如果有的话,一元运算符op+应该是自由的。)重载中不需要参数,因为它是一个成员函数。仅当运算符+是非成员函数时才使用它。@DumbCoder运算符
++
有两个操作数,而不是一个!@Let_Me____Be-我相信它是一元+运算符。如果一元运算符是类成员,则不需要参数。@DumbCoder你是怎么做到这一点的结论:-D@DumbCoder:一元运算符到底是怎么回事?它清楚地执行两个操作数之间的加法。(如果有的话,一元运算符op+应该是自由的。)因此,另一个实现示例可以是:
CVector CVector::operator+(CVector rhs)const{return CVector(x+rhs.x,y+rhs.y);}
因此,另一个实现示例可能是:
CVector CVector::operator+(CVector rhs)const{return CVector(x+rhs.x,y+rhs.y);}
您的声明和定义不匹配。+1[在即将进行编辑之前]为了好的重写。他们现在做:)Thx,我完全跳过了。你的声明和定义不匹配。+1[在你即将编辑之前]为了好的重写。他们现在做:)Thx,我完全跳过了。