如何手动删除类的实例? [英] How do I manually delete an instance of a class?

查看:112
本文介绍了如何手动删除类的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何手动删除类的实例?



示例:

  #include< iostream> 
#include< cstring>

类奶酪{
私人品牌:
字符串品牌;
浮动成本;
public:
Cheese(); //默认构造函数
Cheese(string brand,float cost); //参数化的构造函数
Cheese(const Cheese& rhs); //复制构造函数
〜Cheese(); //析构函数
//等...
}

int main(){
Cheese cheddar( Cabot Clothbound,8.99);
Cheese swiss瑞士( Jarlsberg,4.99);

sw瑞士;
//可以肯定地说 whack不是关键字,
//但我想指出一点。破坏这个实例!

瑞士乳酪(Gruyère,5.99);
//重新实例化瑞士

cout<< \n\n;
返回0;
}


解决方案

不知道用例或您要解决的实际问题(请阅读 XY问题,您的问题就是一个很好的例子)最简单的方法就是重新分配:

  Cheese swiss( Jarlsberg,4.99); 
...
瑞士=奶酪(格鲁耶尔,5.99);

当然,这可能需要您实现赋值运算符,但是要遵循三个或五个规则,您仍然应该这样做(但是,如果您遵循零规则)。



如果您要明确破坏当前的 swiss 对象,您还可以使用指针:

 奶酪*瑞士=新奶酪(贾斯伯格,4.99); 
...
删除瑞士;
swiss = new Cheese(Gruyère,5.99);

但是指针是一罐您应该避免的蠕虫,在现代它并不需要太多C ++。但是,如果要多态性,则需要指针(或引用)。然后,您可以有一个指向基类的指针,该指针指向实际实例,虚拟函数之类的东西将按预期工作。



此外,根据您的情况,我们仍然一无所知,您当然可以使用范围界定:

  Cheese swiss( Jarlsberg,4.99); 
...
{
瑞士乳酪(Gruyère,5.99);
//在这里瑞士奶酪是格鲁耶尔
...
}
//在这里瑞士奶酪是Jarlsberg

尽管像这样对变量名进行阴影处理,还是应该避免的坏习惯,因为这会使代码阅读者感到困惑。另一方面,即使使用范围,也不会阻止您使用所需的任何(有效)变量名,因此您可以将外部范围实例命名为 jarlsberg ,将内部范围实例命名为 gruyere 之后, gruyere 对象将像其他嵌套作用域变量一样在作用域末尾销毁。被破坏并消失。


How do I manually delete an instance of a class?

Example:

#include <iostream>
#include <cstring>

class Cheese {
private:
    string brand;
    float cost;
public:
    Cheese(); // Default constructor
    Cheese(string brand, float cost); // Parametrized constructor
    Cheese(const Cheese & rhs); // Copy construtor
    ~Cheese(); // Destructor
    // etc... other useful stuff follows
}

int main() {
    Cheese cheddar("Cabot Clothbound", 8.99);
    Cheese swiss("Jarlsberg", 4.99);

    whack swiss; 
    // fairly certain that "whack" is not a keyword,
    // but I am trying to make a point. Trash this instance!

    Cheese swiss("Gruyère",5.99);
    // re-instantiate swiss

    cout << "\n\n";
    return 0;
}

解决方案

Without knowing the use-case or the actual problem you want to solve (please read about the XY problem, your question is a good example of it) the simplest way is just reassigning:

Cheese swiss("Jarlsberg", 4.99);
...
swiss = Cheese("Gruyère",5.99);

That might of course require you to implement an assignment operator, but following the rules of three or five you should do that anyway (but the assignment operator is not needed if you follow the rule of zero).

You could also use pointers, if you explicitly want to destroy the current swiss object:

Cheese* swiss = new Cheese("Jarlsberg", 4.99);
...
delete swiss;
swiss = new Cheese("Gruyère",5.99);

But pointers is a can of worms that you should avoid, and don't really need much in modern C++. But pointers (or references) are needed if you want polymorphism. Then you could have a pointer to the base class pointing to the actual instance, and things like virtual functions will work as expected.

Also, and depending on your situation which we still know nothing about, you could of course use scoping:

Cheese swiss("Jarlsberg", 4.99);
...
{
    Cheese swiss("Gruyère",5.99);
    // In here the swiss cheese is a Gruyère
    ...
}
// Out here the swiss cheese is a Jarlsberg

Though shadowing variable names like this works, it's a bad habit that you should avoid as it adds confusion for readers of the code. On the other hand, even when using scopes nothing stops you from using any (valid) variable name you want, so you could name the outer scope instance jarlsberg and the inner scope instance gruyere, the gruyere object would then be destructed at the end of the scope just like any other nested-scope variable would be destructed and "disappear".

这篇关于如何手动删除类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆