当我们在类中定义运算符函数时,我们也在类中定义了它,那么该函数就不是类的一部分。
但当该函数位于类外部并且我们将其声明为类内部的友元但不定义它时,也会实现相同的任务。
考虑一下这段代码,它有两个相同的运算符定义,其中一个在类内部,另一个在类外部:
版本 1(在类内部)
class MyClass
{
// version 1 inside a class
friend MyClass&& operator +(MyClass& a, MyClass& b)
{
return move(MyClass(a.x + b.x, a.y + b.y));
}
int x,y;
public:
MyClass() {}
MyClass(int,int){}
};
int main()
{
MyClass a, b, c;
c = a + b;
cin.ignore();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
版本 2(在类之外)
class MyClass
{
friend MyClass&& operator +(MyClass& a, MyClass& b);
int x,y;
public:
MyClass() {}
MyClass(int,int){}
};
MyClass&& operator +(MyClass& a, MyClass& b)
{
return …Run Code Online (Sandbox Code Playgroud) class Hello {
public static void main(String args[]) {
int i = 10;
i *= i++ + i;
System.out.println(i);
}
}
Run Code Online (Sandbox Code Playgroud)
根据运算符的优先级,后缀运算符的优先级高于乘法运算符。括号内的操作数具有更高的优先级。
首先评估它们: i*(i++ + i) 所以,现在,括号内是 10+11: i*(21)
i 的当前值为 11
其次,由于下一个优先级计算乘法运算符,它不应该是11*21=231吗?
虽然,我知道答案是 210,为什么我上面解释的逻辑是错误的?
我在尝试编译项目时遇到了一些问题。它不断给我消息:“候选函数不可行:'this'参数的类型为'const',但方法未标记为const”。以下是出现此错误的函数。
bool operator<(const node& x) const{
if(name < x.name){
return true;
} else{
return false;
}
}
bool operator==(const node& x){
if(name == x.name){
return true;
} else{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人有任何想法或知道我在使用 const 时出了什么问题,我将非常感激。
我是 kotlin 的新手,我正在为我定义的自定义类处理运算符重载。该类称为“有理数”,表示一个有理数,例如 117/1098。类定义如下,我重载了一堆运算符,如加号、减号、时间等。但是我不确定我必须做什么来重载“in”运算符。
这是我的课:
data class Rational(val rational: String) {
private val numerator: BigInteger
private val denominator: BigInteger
init {
val splitted = rational.split("/")
numerator = splitted[0].toBigInteger()
denominator = when (splitted[1]) {
"0" -> throw Exception("not allowed")
else -> splitted[1].toBigInteger()
}
}
operator fun plus(number: Rational): Rational {
val gcm = denominator * number.denominator
val numerator = (gcm / denominator) * numerator + (gcm / number.denominator) * number.numerator
return Rational("$numerator/$gcm")
}
operator fun minus(number: Rational): Rational {
val gcm …Run Code Online (Sandbox Code Playgroud) 我想使用文章中&&示例所描述的短路评估语法(带运算符):
说,我有这种情况:
function externalFunction() {
id == ...
text == ...
// OK: Standard if syntax is fine
if ( aFunction(id, text) ) return
// KO: Short circuit evaluation generate a RUN-TIME ERROR:
// SyntaxError: Unexpected token 'return'
// anotherFunction(id, text) && return
// ^^^^^^
anotherFunction(id, text) && return
}
Run Code Online (Sandbox Code Playgroud)
为什么我有错误?也许我不能使用带有语言关键字 ( return)的单个语句?
javascript if-statement expression-evaluation node.js operator-keyword
我正在对热传递中的 LMTD 方法进行编码,并且在同时使用 和 运算符两次以上时遇到了一些错误。
if (del_T1 > beta) and (del_T2 > beta) and (del_T1<>del_T2) then
T_LMTD = (del_T1-del_T2) / ( (log(del_T1)) - (log(del_T2)));
elseif (del_T1 > beta) and (del_T2 > beta) and (del_T1==del_T2) then
T_LMTD = (del_T1-del_T2) / 2;
elseif (del_T1 > beta) and (del_T2 < beta) then
T_LMTD = (del_T1 - beta) / ( (log(del_T1/beta)) * (1 - zeta * (del_T2 - beta)));
elseif (del_T1 < beta) and (del_T2 > beta) then
T_LMTD = (del_T2 - beta) / …Run Code Online (Sandbox Code Playgroud) 我需要重载这个运算符(mainMenu 是名为“Menu”的类类型):
if (mainMenu) {
cout << "The mainMenu is valid and usable." << endl;
}
Run Code Online (Sandbox Code Playgroud)
我试过这个,但没有用:
bool operator!(const Menu& lobj);
Run Code Online (Sandbox Code Playgroud) 我试图将列表中的所有数字彼此相乘
a = [2,3,4]
for number in a:
total = 1
total *= number
return total
Run Code Online (Sandbox Code Playgroud)
输出应该是 24,但由于某种原因我得到 4。为什么会这样?
我有一个名为的自定义结构StorageFile,其中包含有关存储在数据库中的文件的一些信息。
class StorageFile : public QObject {
Q_OBJECT
public:
int ID = -1;
QString filename;
QString relativeLocation;
MediaType type;
QDateTime dateLastChanged;
quint64 size;
QString sha256;
//...
explicit StorageFile(QObject* parent = nullptr);
StorageFile(const StorageFile& other);
StorageFile& operator= (const StorageFile& other);
bool operator< (const StorageFile& storageFile); < --------- implemented operator
bool operator== (const StorageFile& storageFile);
//...
}
Run Code Online (Sandbox Code Playgroud)
我将这些 StorageFile 对象添加到QMap,并且QMap 需要实施operator< - 我有。编译器给出以下错误:
F:\Qt\Qt5.13.1\5.13.1\mingw73_32\include\QtCore/qmap.h:71:17: error: no match for 'operator<' (operand types are 'const StorageFile' and …Run Code Online (Sandbox Code Playgroud) 我读过加号运算符将其右值添加到其左值中。例如,如果我们编写x + 1;加号运算符x,在内存中查找变量并添加1到其中。
但是这个运算符不是这样工作的,因为在下面的代码中,它不会添加1到它的左值 ( x)。
int x = 4;
x + 1;// now the + operator adds 1 to x variable.
std::cout << x << std::endl;// this line must print 5 but doesn't.
Run Code Online (Sandbox Code Playgroud)
如果它不工作,就像我怎么解释,那怎么不工作?
operator-keyword ×10
c++ ×5
if-statement ×2
class ×1
constants ×1
dymola ×1
friend ×1
function ×1
java ×1
javascript ×1
kotlin ×1
list ×1
math ×1
modelica ×1
node.js ×1
overloading ×1
python ×1
qmap ×1
qt ×1