- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
作为一般性的开放性问题,它可以表述为为什么有时会在代码中间调用析构函数?!有哪些不同的可能场景可以调用析构函数。?我想在下面的代码中理解类似的效果
class complex
{
private:
double re, im;
protected:
public:
complex()
{
cout << "def const " << endl;
}
complex(double r, double i)
{
cout << "parameterized " << endl;
re = r;
im = i;
}
void setdata(double r, double i)
{
re = r;
im = i;
}
void getdata()
{
cout << "enter real" << endl;
cin >> re;
cout << "enter im" << endl;
cin >> im;
}
//there are 3(?) possible variants of addition...check
//1st
void add(complex c1, complex c2)
{
cout << "in add" << endl;
re = c1.re + c2.re;
im = c1.im + c2.im;
}
//2nd
void add(complex c)
{
cout << "in add" << endl;
re += c.re;
im += c.im;
}
//3rd --- (???)
// complex add(complex c1, complex c2)
// {
// complex retc;
// retc.re = c1.re + c2.re;
// retc.im = c1.im + c2.im;
//
// return retc; //this one is very weird
// }
void display()
{
cout << endl << re << " + " << im << "i" << endl;
}
void mul(complex c1, complex c2)
{
cout << "in mul" << endl;
re = c1.re*c2.re - c1.im*c2.im;
im = c1.re*c2.im + c1.re*c2.im;
}
complex mul(complex c)
{
cout << "in mul" << endl;
complex retc;
retc.re = re*c.re - im*c.im;
retc.im = re*c.im + c.re*im;
return retc;
}
~complex()
{
cout << re << " + " << im << "i" << endl;
cout << "dest" << endl;
}
};
int main()
{
complex c1;
c1.getdata();
complex c2(5, 5);
complex c3;
c3.add(c1, c2); //to store the answer of c1 + c2 we need c3 object
c3.display();
//perform c1 + c2 * c3
complex c4;
c4.add(c1, c2.mul(c3)); //can not use mul(c2, c3) for c2 * c3...why???!
cout << "ans1" << endl;
c4.display();
//or we can also do...
c1.add(c2.mul(c3)); //but this will modify c1
cout << "ans2" << endl;
c1.display();
return 0;
}
输出如下
def const
enter real
1
enter im
2
parameterized
def const
in add
1 + 2i
dest
5 + 5i
dest
6 + 7i
def const
in mul
def const
in add
1 + 2i
dest
-5 + 65i
dest
6 + 7i
dest
ans1
-4 + 67i
in mul
def const
in add
-5 + 65i
dest
6 + 7i
dest
ans2
-4 + 67i
-4 + 67i
dest
6 + 7i
dest
5 + 5i
dest
-4 + 67i
dest
知道为什么析构函数会在不知不觉中被调用吗!?
最佳答案
why some times destructor gets called in the middle of the code?
以这些方法为例:
void mul(complex c1, complex c2)
{
cout << "in mul" << endl;
re = c1.re*c2.re - c1.im*c2.im;
im = c1.re*c2.im + c1.re*c2.im;
}
void add(complex c1, complex c2)
{
cout << "in add" << endl;
re = c1.re + c2.re;
im = c1.im + c2.im;
}
你调用它:
c4.add(c1, c2.mul(c3));
add()
和 mul()
都按值接收它们的参数。当编译器看到按值传递的参数时,它会通过复制构造函数创建一个新版本的参数对象。所有的类都有一个默认的拷贝构造函数,它将每个字段成员一一赋值。这个新版本在整个方法中被使用,最后被销毁。
因此,当您在 c2 中以 c3 作为参数调用 mul()
时,一个新的复杂 对象从 c3 创建,当到达 mul()
的末尾时,它被销毁。使用 c1 调用 add()
和 c2.mul(c3)
的结果也是如此。
如果您想避免这种复制(这需要时间和资源),那么您应该更改函数中传递的参数类型。具体来说,您可以通过指针或引用传递它们。问题是这将允许在函数内部修改它们:但在传递引用的具体情况下,您可以使用 const
修改它,这让您两全其美:您使对象有效地传递而没有修改的可能性。
void mul(const complex &c1, const complex &c2)
{
cout << "in mul" << endl;
re = c1.re*c2.re - c1.im*c2.im;
im = c1.re*c2.im + c1.re*c2.im;
}
void add(const complex &c1, const complex &c2)
{
cout << "in add" << endl;
re = c1.re + c2.re;
im = c1.im + c2.im;
}
考虑到上述方法不会修改调用它们的实例,它们本身也可以是 const
。
void mul(const complex &c1, const complex &c2) const
{
cout << "in mul" << endl;
re = c1.re*c2.re - c1.im*c2.im;
im = c1.re*c2.im + c1.re*c2.im;
}
void add(const complex &c1, const complex &c2) const
{
cout << "in add" << endl;
re = c1.re + c2.re;
im = c1.im + c2.im;
}
此外,由于这些函数不需要复杂 的实例,它们也可以是独立的friend
函数或static
方法。实际上,这值得另一个答案。
根据经验,当您只有一个属于同一类的参数时,如在 void mul(complex c2)
中,它可能是该类的成员;您将以 c1.mul( c2 )
的形式调用它。当你有两个参数时,如 void mul(complex c1, complex c2)
那么它是一个独立的函数(即,你将调用它作为 mul( c1, c2 )
,如果你想把它包装在你的类中或访问你的类的私有(private)成员,它可以是友元。通常你创建这些友元函数是因为你有一个运算符和另一个类(或原语)的对象在它的左。另一个问题是独立函数最好返回一个新对象,而不是修改它的参数之一……如您所见,它变得越来越复杂。
无论如何,这些是您的方法应该使用的签名:
class complex {
public:
// ... more things...
void mul(complex c2);
complex operator*(const complex &c2);
friend complex operator*(int x, const complex &c1);
// ... more things...
};
此外,与其使用 display()
函数将您的类绑定(bind)到控制台,不如重载运算符 <<,这样您就可以在任何流中使用该功能。
希望这对您有所帮助。
关于C++,构造函数,析构函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48782408/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!