简体   繁体   English

C++:析构函数和指针

[英]C++ : Destructor and pointers

In my class C, there is a pointer (var_a) to a class A, so in the destructor of C, I write "delete var_a". In my class C, there is a pointer (var_a) to a class A, so in the destructor of C, I write "delete var_a". In vscode, the code works but doesn't stop automatically after the end of the main.在 vscode 中,代码可以工作,但不会在 main 结束后自动停止。 Also, the line where var_a is deleted, is highlighted in yellow.此外,删除 var_a 的行以黄色突出显示。 The debug console print:调试控制台打印:

Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
=cmd-param-changed,param="pagination",value="off"

The hpp: hpp:

#ifndef DEF_TEST4
#define DEF_TEST4

#include <iostream>
#include <string>


class A
{   public:
    A();
    A(A const& copy_a);
    virtual std::string printer();
    protected:
    std::string var;
};

class B : public A
{
    public:
    B();
    B(B const& copy_b);
    virtual std::string printer();
    protected:
    std::string var;
};

class C
{
    public:
    C(A* a);
    ~C();
    virtual A* get_a();
    protected:
    A* var_a;
};

#endif

The cpp: cp:

#include "test4.hpp"


A::A() : var("a")
{}

B::B() : var("b")
{}


A::A(A const& copy_a) : var(copy_a.var)
{}

B::B(B const& copy_b) : var(copy_b.var)
{}


std::string A::printer()
{
    return var;
}

std::string B::printer()
{
    return var;
}

C::C(A* a) : var_a(a)
{}

C::~C()
{
    delete var_a;
}

A* C::get_a()
{
    return var_a;
}

The main cpp:主要cpp:

#include "test4.hpp"
#include "test4.cpp"
#include <typeinfo>

int main()
{
    A ca;
    B cb;
    B cb2(cb);
    C cc(&ca);
    C cc2(&cb);

    std::cout << ca.printer() << std::endl;
    std::cout << cb.printer() << std::endl;
    std::cout << cb2.printer() << std::endl;
    std::cout << cb2.A::printer() << std::endl;
    std::cout << cc.get_a()->printer() << std::endl;
    std::cout << cc2.get_a()->printer() << std::endl;
    std::cout << "type cc2.get_a() : " << &typeid(cc2.get_a()) << std::endl;
    std::cout << "type ca : " << &typeid(ca) << std::endl;
    std::cout << "type cb : " << &typeid(cb) << std::endl;

    cc.~C();

}

I suppose that there is a problem, but what?我想有问题,但是什么? Sorry for the possible bad english, it's not my mother tongue.抱歉,英语可能不好,这不是我的母语。 Thanks for your help.谢谢你的帮助。

As the comments have suggested, there are two problems in the code.正如评论所暗示的,代码中有两个问题。

First, cc.~C() : don't ever call destructors explicitly.首先, cc.~C() :永远不要显式调用析构函数。 There are situations where that's appropriate, but those are far in your future.在某些情况下这是合适的,但在你的未来很远。 The compiler will call the destructor when cc goes out of scope.cc超出 scope 时,编译器将调用析构函数。 You don't need to do it yourself.你不需要自己做。

Second, in cc and cc2 , var_a points at an object that was not created with new ;其次,在cccc2中, var_a指向不是用new创建的 object ; don't delete it.不要delete它。 Just remove that delete var_a;只需删除delete var_a; from C::~C() .来自C::~C() The objects that var_a points at will also be destroyed when they go out of scope. var_a指向的对象在 go 超出 scope 时也会被销毁。 You don't need to do it yourself.你不需要自己做。

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

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