在此上下文中受保护的破坏者C++ (第1期)

发布于 2025-01-25 20:43:04 字数 2435 浏览 4 评论 0原文

我有C ++ 11的汇编误差。

我已经用受保护的 ctor和dtor定义了基类节点。我已经用 public 继承定义了一个继承的类目录

我称其为继承类的DTOR中的基类的DTOR。 该消息的汇编失败了:

error: 'virtual Node::~Node()' is protected within this context

我真的不明白为什么,因为我很确定受派生类中的关键字允许的方法(和对象)可以访问public < /code>和受保护的基类的方法(以及数据)。

在下面,我将代码放置了,我希望有足够的评论。这个想法是创建在RAM中运行的基本FS。

node.hpp

#ifndef NODE_HPP
#define NODE_HPP

#include "FileSystem.hpp"

class Directory;    //Forward declaration of class Directory used in this class
class File;         //Forward declaration of class File used in this class

/*Class representing a node of the fs : a node can be a file or a directory*/
class Node
{
    /*friend class Directory;*/     //Related to the issue #1 : Destructor protected within this context

    protected:
        Node(FileSystem *, int, string, Directory * = nullptr);
        virtual ~Node();
        Node(const Node&);

    ...

#endif // NODE_HPP

node.cpp

#include "Node.hpp"

Node::Node(FileSystem *fs, int uid, string name, Directory *parent) {
    this->fs = fs;
    this->uid = fs->get_fresh_uid();
    this->name = name;
    this->parent = parent;
}

Node::~Node(){
    fs = nullptr;
    parent = nullptr;
}

...

directory.hpp

#ifndef DIRECTORY_HPP
#define DIRECTORY_HPP

#include "Node.hpp"
#include "File.hpp"
#include <vector>

/*Class representing a directory of the fs : a directory is a kind of node*/
class Directory : public Node
{
    /*friend class FileSystem;*/ //Related to issue #2 : forward declaration of class Directory

    private:
        Directory(FileSystem *fsys, int id, string n, Directory *par = nullptr);
        virtual ~Directory();
        Directory (const Directory &);
    private:
        vector<Node*> children;             //Vector representing the children nodes of the directory
    ...
};

#endif // DIRECTORY_HPP

directory.cpp

#include "Directory.hpp"

Directory::Directory(FileSystem *fs, int uid, string name, Directory *parent) : Node(fs, uid, name, parent){
}

Directory::~Directory(){
    for (auto c : children) {
        delete c;   //Related to the issue #1 : Destructor protected within this context
        c=nullptr;
    }
}

...

I have a compilation error with C++11.

I have defined a base class Node with protected ctor and dtor. I have defined an inherited class Directory with public inheritance.

I am calling the dtor of the base class in the dtor of the inherited class.
The compilation failed with the message:

error: 'virtual Node::~Node()' is protected within this context

I really don't understand why, as I was pretty sure that the protected keyword allowed methods (and objects) in a derived class to have access to public and protected methods (and data) of the base class.

Below I have put the code, that I hope has enough comments. The idea is to create a basic fs running in RAM.

Node.hpp

#ifndef NODE_HPP
#define NODE_HPP

#include "FileSystem.hpp"

class Directory;    //Forward declaration of class Directory used in this class
class File;         //Forward declaration of class File used in this class

/*Class representing a node of the fs : a node can be a file or a directory*/
class Node
{
    /*friend class Directory;*/     //Related to the issue #1 : Destructor protected within this context

    protected:
        Node(FileSystem *, int, string, Directory * = nullptr);
        virtual ~Node();
        Node(const Node&);

    ...

#endif // NODE_HPP

Node.cpp

#include "Node.hpp"

Node::Node(FileSystem *fs, int uid, string name, Directory *parent) {
    this->fs = fs;
    this->uid = fs->get_fresh_uid();
    this->name = name;
    this->parent = parent;
}

Node::~Node(){
    fs = nullptr;
    parent = nullptr;
}

...

Directory.hpp

#ifndef DIRECTORY_HPP
#define DIRECTORY_HPP

#include "Node.hpp"
#include "File.hpp"
#include <vector>

/*Class representing a directory of the fs : a directory is a kind of node*/
class Directory : public Node
{
    /*friend class FileSystem;*/ //Related to issue #2 : forward declaration of class Directory

    private:
        Directory(FileSystem *fsys, int id, string n, Directory *par = nullptr);
        virtual ~Directory();
        Directory (const Directory &);
    private:
        vector<Node*> children;             //Vector representing the children nodes of the directory
    ...
};

#endif // DIRECTORY_HPP

Directory.cpp

#include "Directory.hpp"

Directory::Directory(FileSystem *fs, int uid, string name, Directory *parent) : Node(fs, uid, name, parent){
}

Directory::~Directory(){
    for (auto c : children) {
        delete c;   //Related to the issue #1 : Destructor protected within this context
        c=nullptr;
    }
}

...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左耳近心 2025-02-01 20:43:04

感谢您的评论和答案的链接。

Thanks for the comments and the link to the answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文