gpt4 book ai didi

C++,构造函数,析构函数

转载 作者:行者123 更新时间:2023-11-30 03:25:48 24 4
gpt4 key购买 nike

作为一般性的开放性问题,它可以表述为为什么有时会在代码中间调用析构函数?!有哪些不同的可能场景可以调用析构函数。?我想在下面的代码中理解类似的效果

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)到控制台,不如重载运算符 <<,这样您就可以在任何流中使用该功能。

找到 complete code in IDEOne .

希望这对您有所帮助。

关于C++,构造函数,析构函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48782408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com