c++ - 前向声明是行不通的

标签 c++ forward-declaration

下面是两个代码片段(准备编译)。在第一个片段中,我仅使用结构的前向声明,同时未调用从 Guest 类的基类 dtor 中删除指向该结构的指针。
在第二个片段中,我没有使用前向声明,而是使用了这个 Guest 类的完整定义,使用了 Base 中的 delete 按预期工作。
为什么?为什么会有所作为?前向声明难道不应该只是编译器的注释,说明此类/结构的定义在其他地方吗?
我很惊讶它不能直观地工作。

//First just forward dclr  
#include "stdafx.h"
#include <iostream>
using std::cout;

struct Guest;

struct Base
{
    Guest* ptr_;
    Base(Guest* ptr):ptr_(ptr)
    {
        cout << "Base\n";
    }
    ~Base()
    {
        cout << "~Base\n";
        delete ptr_;
    }
};

struct Guest
{
    Guest()
    {
        cout << "Guest\n";
        throw std::exception();
    }
    Guest(int)
    {
        cout << "Guest(int)\n";
    }
    ~Guest()
    {
        cout << "~Guest\n";
    }
};

struct MyClass : Base
{
    Guest g;
    MyClass(Guest* g):Base(g)
    {
        cout << "MyClass\n";

    }
    ~MyClass()
    {
        cout << "~MyClass\n";
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    try
    {
        Guest* g = new Guest(1);
    MyClass mc(g);
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what();
    }
    return 0;
}

//第二 - 全定义

#include "stdafx.h"
#include <iostream>
using std::cout;

struct Guest
{
    Guest()
    {
        cout << "Guest\n";
        throw std::exception();
    }
    Guest(int)
    {
        cout << "Guest(int)\n";
    }
    ~Guest()
    {
        cout << "~Guest\n";
    }
};

struct Base
{
    Guest* ptr_;
    Base(Guest* ptr):ptr_(ptr)
    {
        cout << "Base\n";
    }
    ~Base()
    {
        cout << "~Base\n";
        delete ptr_;
    }
};



struct MyClass : Base
{
    Guest g;
    MyClass(Guest* g):Base(g)
    {
        cout << "MyClass\n";

    }
    ~MyClass()
    {
        cout << "~MyClass\n";
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    try
    {
        Guest* g = new Guest(1);
    MyClass mc(g);
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what();
    }
    return 0;
}

最佳答案

来自 C++ 标准 (5.3.5/5):

If the object being deleted has incomplete class type at the point of deletion and the complete class has a non-trivial destructor or a deallocation function, the behavior is undefined.

因此您不能对不完整的类型使用 delete。它会调用析构函数,而编译器还不知道。

关于c++ - 前向声明是行不通的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4023794/

相关文章:

c++ - 我在函数中增加指针,但他在退出函数后返回到以前的位置

c++ - 优化

c++ - 从 ini 文件中读取十六进制字符

python - python 在 gdb 调试器中做了什么?

c++ - 无法访问在标准回调中传递的对象变量

c++ - 不完整类型的无效使用 "class"

c++ - 声明但不定义内部结构/类——C++ 是否合法?

c - C 中有什么方法可以在头文件中转发声明结构而不必在其他文件中使用指针?

C++通过制作索引数组进行排序

c++ - 前向声明类时成员函数的行为