我正在使用静态变量来跟踪对象实例的数量。我使用以下代码得到了一些不可预测的行为:
#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/