简体   繁体   English

如何对对象使用指针; 构造函数和析构函数。 C++

[英]How to use pointer with objects; Constructors and Destructors. C++

I'm trying to make an output of creation and deletion of an object in specific order.我正在尝试按特定顺序创建和删除对象的输出。 Using constructors destructors by default first it creates the first object and so on, and destructor starts deletion from last created object.默认情况下,首先使用构造函数析构函数创建第一个对象,依此类推,析构函数从最后创建的对象开始删除。 I'm trying to change the output order something similar to this:我正在尝试更改类似于此的输出顺序:

Object 1 is created, we've got 1 object(s) now!

Object 2 is created, we've got 2 object(s) now!

Object 3 is created, we've got 3 object(s) now!

Object 3 is deleted, we've got 2 object(s) now!

Object 2 is deleted, we've got 1 object(s) now!

Object 4 is created, we've got 2 object(s) now!

Object 4 is deleted, we've got 1 object(s) now!

Object 1 is deleted, we've got 0 object(s) now!  

Code:代码:

class Object {
public:
    Object(int i) {
        id = i;
        count++;
        cout<<"Object "<<id<<" is created, we've got "<<count<<" object(s) now!"<<endl;
    }
    ~Object() {
        count--;
        cout<<"Object "<<id<<" is deleted, we've got "<<count<<" object(s) now!"<<endl;
    }
private: 
    int id;
    static int count;
};  

In order to do that i found out that i can use pointers, pointing to the created object and delete that when we want, in this way we can control the order of creation and deletion.为了做到这一点,我发现我可以使用指针,指向创建的对象并在需要时删除它,这样我们就可以控制创建和删除的顺序。

#include"Object.h"
extern void TestObjects()
    {

    Object *a;
    Object obj1(1),obj2(2),obj3(3);

    a = &obj3;
    delete a;

    Object *c;
    c = &obj2;
    delete c;

    }

Till now it only creates 3 objects and deletes the third one immediately after creation, and I'm trying to delete the second right after the deletion of third.到目前为止,它只创建了 3 个对象,并在创建后立即删除了第三个对象,我试图在删除第三个对象后立即删除第二个对象。

Your use of delete is invalid, causing undefined behavior , because you are not using new to create the objects.您使用delete无效,导致未定义行为,因为您没有使用new创建对象。 You are trying to destroy objects that exist on the stack and are destroyed automatically when they go out of scope, you have no right to destroy them manually.您正在尝试销毁堆栈中存在的对象,并且当它们超出范围时会自动销毁,您无权手动销毁它们。

If you want to use delete to control destruction order, you MUST use new , like this:如果你想使用delete来控制销毁顺序,你必须使用new ,像这样:

void TestObjects()
{
    Object *obj1 = new Object(1);
    Object *obj2 = new Object(2);
    Object *obj3 = new Object(3);

    delete obj3;
    delete obj2;

    Object *obj4 = new Object(4);
    delete obj4;

    delete obj1;
}

Live Demo现场演示

Alternatively, in your particular example, it is possible to get the same output by simply taking more control of the objects' scopes and not using new and delete at all:或者,在您的特定示例中,只需更多地控制对象的范围并且根本不使用newdelete ,就可以获得相同的输出:

void TestObjects()
{
    Object obj1(1);

    {
    Object obj2(2);
    Object obj3(3);
    } // <-- obj3 and obj2 are destroyed here, in that order!

    Object obj4(4);
} // <-- obj4 and obj1 are destroyed here, in that order!

Live Demo现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM