这是代码:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <utility>
struct student
{
std::string full_name;
int group;
int number;
friend std::istream& operator>>(std::istream& in, student& obj)
{
in >> obj.full_name >> obj.group >> obj.number;
return in;
}
friend std::ostream& operator<<(std::ostream& out, const student& obj)
{
out << "Full name: " << obj.full_name << '\n';
out << "Group: " << obj.group << '\n';
out << "Number: " << obj.number << '\n';
return out;
}
bool operator<(const student& obj)
{
return this->number …Run Code Online (Sandbox Code Playgroud) Python 3 允许重载*.
例如,
a*b
允许a通过提供方法来定义结果__mul__。
然而,如果该方法返回,则调用NotImplemented,该__rmul__方法b(如果存在)以给出结果。此处记录了这一点。
这样做的一个优点是我可以在b不a知道a.
C++ 中是否有等效的方法__rmul__,或者是否有实现相同或类似事物的方法?
假设我有一个非常简单的 Rational 类,如下所示:
class Rational
{
int numer;
int denom;
public:
Rational(const int& numer, const int& denom) : numer(numer), denom(denom) {}
void operator*(const Rational& other) { std::cout << "multiply, simple as\n"; }
}
Run Code Online (Sandbox Code Playgroud)
一切都很好。然后说我希望能够将 Rational 类与整数相乘,因此我向该类添加另一个函数:
class Rational
{
int numer;
int denom;
public:
Rational(const int& numer, const int& denom) : numer(numer), denom(denom) {}
void operator*(const Rational& other) { std::cout << "multiply, simple as\n"; }
void operator*(const int& other) { std::cout << "some math here\n"; }
}
Run Code Online (Sandbox Code Playgroud)
好吧,没什么大不了的。除了我实际上无法执行以下操作,因为参数的顺序都是错误的:
Rational mine(1, …Run Code Online (Sandbox Code Playgroud) 我有一个名为“Vector”的类。它由两个私有字段组成:std::vector<double> coordinates和int len。方法dim()返回len。
我<<像这样重载运算符:
friend std::ostream& operator<<(std::ostream& os, Vector& vec )
{
std:: cout << "(";
for ( int i = 0; i < vec.dim(); i++ ) {
if ( i != vec.dim()-1){
os << vec[i] << ", ";
} else {
os << vec[i];
}
}
os << ')';
return os;
}
Run Code Online (Sandbox Code Playgroud)
+像这样的运算符:
friend Vector operator +(Vector& first, Vector& second)
{
if(first.dim() != second.dim()){
throw std::length_error{"Vectors must be …Run Code Online (Sandbox Code Playgroud) 我是 C++ 新手,我正在编写一个程序来计算输入的年份是否为闰年,但这会返回一个错误,提示:无法重载仅按返回类型区分的函数。而且,不接受带有布尔表达式的运算符“==”。为什么?谢谢。这是我的代码:
using namespace std;
int leap_year(int year);
bool div(int num, int divid) {
if (num % divid == 0)
return true;
else
return false;
}
int leap_year(int year) {
if (div(year, 400))
return 365;
else if (div(year, 100) & div(year, 400) == false)
return 366;
else
return 365;
}
void main() {
int year;
cout << "Year: ";
cin >> year;
int result = leap_year(year);
cout << result;
system("pause");
}
Run Code Online (Sandbox Code Playgroud) 我有一个函数,其当前签名是f(a,b = 0).我想添加另一个参数c.我希望我的函数能够以这种方式调用f(a,b),这是当前行为和f(a,c).一种方法是重载函数并复制功能代码.我不想从f(a,c)调用f(a,b).我在C++工作.
是否有任何标准设计模式或解决方案可以避免此代码重复?
我有这两个功能
private void calcResults()
{
MakePath(id, results, _resultCount);
MakePath(id, "XYZ", _resultSICount)
}
private string MakePath(string subFolder, object obj, int index)
{
string dir = System.IO.Path.Combine(_outputDir, subFolder);
string fileName = string.Format("{0} {1} {2}.xml",
obj.GetType().Name, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString());
return System.IO.Path.Combine(dir, fileName);
}
private string MakePath(string subFolder, string tempFileName, int index)
{
string dir = System.IO.Path.Combine(_outputDir, subFolder);
string fileName = string.Format("{0} {1} {2}.xml",
tempFileName, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString());
return System.IO.Path.Combine(dir, fileName);
}
Run Code Online (Sandbox Code Playgroud)
请帮助一下.
谢谢
我在我的程序中遇到了重载运算符"+"," - ","/"和"*"的问题.基本上我有一个对象,它是动态分配的double值数组.我有整个程序,但我无法克服那些重载.
我的构造函数看起来像这样:
table::table(int size) {
this->size = size;
tab = new double[size];
count++;
}
Run Code Online (Sandbox Code Playgroud)
我写过类似的东西:
table & table::operator-(const table &tab3 )
{
table * tab_oper2 = new table(size);
for(int i=0; i< tab3.size; i++)
{
(*this).tab[i] -= tab3.tab[i];
}
return *this;
}
Run Code Online (Sandbox Code Playgroud)
这通常是有效的,但通常不是这样做的好方法.我的导师告诉我尝试将(*this)切换到*tab_oper2,将其作为参考返回,但它不起作用.拜托,有人能告诉我怎么做得好吗?
int i;
Integer a; //a class object
i=a; //here a's member variable value should be assigned to 'i'
Run Code Online (Sandbox Code Playgroud) 如果我有一个Base和一个派生类:
class Base {
//...
};
class Derived : public Base {
//...
};
Run Code Online (Sandbox Code Playgroud)
是否可以通过以下方式重载功能?
void DoSomething(Base b) {
cout << "Do Something to Base" << endl;
}
void DoSomething(Derived d) {
cout << "Do Something to Derived" << endl;
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做会发生什么:
int main() {
Derived d = Derived();
DoSomething(d);
}
Run Code Online (Sandbox Code Playgroud)
Derived也是一个Base ..所以调用哪个版本?
overloading ×10
c++ ×9
c# ×1
class ×1
inheritance ×1
leap-year ×1
max ×1
overriding ×1
polymorphism ×1
refactoring ×1
std ×1
vector ×1