- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用一个静态变量来跟踪一个对象的实例数。我使用以下代码得到了一些不可预知的行为:
#include <iostream>
using namespace std;
class comp{
private:
int a;
int b;
public:
friend comp &operator+(comp, comp);
static int c;
comp(int, int);
comp();
~comp();
int getA();
int getB();
void print() const;
};
int comp::c = 0;
comp::~comp() {
c--;
cout << "destroying comp of " << a << ", " << b << ", count after decrement is " << c << endl;
}
comp &operator+(comp A, comp B){
comp C = comp(A.a + B.a, A.b + B.b);
cout << "in +, count is " << comp::c << endl;
return C;
}
comp::comp(int A, int B){
a = A;
b = B;
c++;
}
comp::comp(){
a = 0; b = 0; c++;
}
int comp::getA(){
return a;
}
int comp::getB(){
return b;
}
void comp::print() const{
cout << a << ", " << b << endl;
}
int main()
{
cout << comp::c << endl;
comp A = comp(3,4);
cout << comp::c << endl;
comp B = comp(4,5);
cout << comp::c << endl;
A + B;
A.print();
B.print();
cout << "About to exit main, c is: " << comp::c << endl;
return 0;
}
输出是这样的:
0
1
2
in +, count is 3
destroying comp of 7, 9, count after decrement is 2
destroying comp of 3, 4, count after decrement is 1
destroying comp of 4, 5, count after decrement is 0
3, 4
4, 5
About to exit main, c is: 0
destroying comp of 4, 5, count after decrement is -1
destroying comp of 3, 4, count after decrement is -2
此行为是由线路引起的
A + B;
我对正在发生的事情的最佳猜测是,当对象作为参数传递给函数时,构造函数没有被调用(因此 c 没有递增),但是当函数超出范围时,析构函数被调用。这导致计数的净损失。
但是,重载的 operator+ 通过引用传递变量。这不会阻止参数超出范围和调用析构函数吗?
无论是否将重载的 + 运算符声明为:
,输出都是相同的friend comp &operator+(comp, comp);
或
friend comp operator+(comp, comp);
这让我对为什么以及如何调用析构函数感到困惑。
最后一个问题:使用重载运算符时,按引用传递而不是按值传递是好的做法吗?如果是这样,为什么?
谢谢!
编辑:看起来好像我有点混淆了语法。我以为
friend comp &operator+(comp, comp);
通过引用而不是
传递参数friend comp operator+(comp&, comp&);
请原谅我的问题,有人能解释一下函数名前的“&”运算符是做什么的吗?如果我理解正确的话,“&”是引用运算符,并给出给定变量的地址。但是,如果想要返回一个引用,“*”运算符将是合适的。例如:
int *a()
上面的函数签名返回一个指向 int 的内存地址。
int &a()
函数签名会返回什么?
最佳答案
您没有考虑默认创建的对象,编译器生成的复制构造函数。定义一个,计算在那里创建的对象,数学就会计算出来。
关于c++ - 当作为参数传递的对象超出范围时,析构函数会调用自身吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20828946/
在文档中我们可以找到 The limits are based on a moving window that tracks the number of requests you send per h
我试图了解使用 Windows Azure 托管 Web 服务的正确方法。在阅读了一些可用的文档后,我已经达到以下几行: Windows Azure takes the following actio
我正在使用 unboundid ldap sdk 来执行 ldap 查询。运行 ldap 搜索查询时遇到一个奇怪的问题。当我对包含 50k 个条目的组运行查询时出现异常。我的异常(exception)
我有以下 docker-compose 文件: version: "2.4" services: auto_check: image: python mem_limit: 97M
我有副本集(托管在亚马逊上),其中有: 主要 中学 仲裁者 它们都是 3.2.6 版本,这个副本正在我的分片集群中创建一个分片(如果这很重要,尽管我认为它不重要)。 当我在 primary 上键入 r
我知道在 C++ 中访问缓冲区边界是未定义的行为。 这是来自 cppreference 的示例: int table[4] = {}; bool exists_in_table(int v) {
嗨,我有一个表单的 div。我希望当鼠标离开 div 时禁用单击事件。所以我尝试了这个,但它不起作用,div 仍然可以点击。有什么想法吗?? var flag = false; $("#foo").l
我正在使用我的客户端获取有关存储在我的 Swift 对象存储中的某个文件的一些信息,该文件可以通过 REST Api 访问。在 Swift 中,指向指定对象的 HEAD 方法和 url 返回它的元数据
如何在 Excel 的 CONCATENATE 函数中使用超过 255 个字符?我实际上也在 EXCEL 的 HYPERLINK 函数中使用 CONCATENATE 函数。一个例子如下: =HYPER
在 java 6 web 应用程序中,我尝试从执行的命令中检索大量输出。我在 javaworld article 上“借用/窃取/基于”它。我面临的问题是,由于输出被截断,长度似乎超出了大小限制。我已
我有一个更改事件,当选择框更改时会触发该事件。然而,选择框位于被替换的 div 内,因此会重新生成选择框。由于此错误可能是由于无限循环造成的,因此我猜测创建选择框时也必须触发我的触发事件。我尝试了很多
我正在 visual studio 2013 中用 c# 创建一个网络服务。我已连接到数据库并使用以下代码返回 json。 [WebMethod] [ScriptMethod(ResponseForm
我使用 php 脚本解析远程 xml 文件并将网页上的输出打印到 div 中。由于我需要输出必须与当前播放的轨道同步,所以我使用 Javascript 每 20 秒重新加载一次 div 内容。在测试页
#define MAX_BUFF_SIZE 64 char input[MAX_BUFF_SIZE]; int inSize = read(0, input, MAX_BUFF_SIZE); if
我在申请公司时遇到了问题。 我将总结系统的关键要素: 我公司的系统几年前就在 Windows XP 和 7(家庭版、专业版、基本版)机器上运行。 它是用 .NET 4.0 编写的,基于 WCF。 它使
我有一个渲染循环,用于监听数位板输入并从顶点/索引缓冲区(以及其他内容)中绘制。顶点数据可以增长,当它达到一定水平时,DispatchMsg(&msg) 会遇到这种情况: Unhandled exce
我通过 Postgres JDBC 驱动程序使用 Java 1.7 和 Postgres。将从 Web 服务使用数据库连接。在测试中,我得到了以下错误: FATAL: connection limit
我想知道当超过 Firebase 实时数据库的限制时会发生什么。问题是我知道我可以拥有的最大连接数仅为 100。现在,假设我的 Android 应用程序有 1,000 个活跃用户,并且我实现了实时数据
我正在将一组图像上传到我的 node.js Express 服务器,但收到错误 - “错误:超出 maxFieldsSize”。看起来默认的 maxFieldsSize 是 2MB。我需要能够上传最多
我正在使用 Django 构建一个小型 Web 项目,该项目有一个包含 ImageField 的模型 (Image)。当我尝试使用管理界面上传图片时,我遇到了这个问题(删除了个人身份信息): Runt
我是一名优秀的程序员,十分优秀!