简体   繁体   English

如何在C ++的类中创建嵌套组织?

[英]How do you create nested organization in a class for c++?

I'm fairly new to c++, but I've reached a point where I want to build my own library for use in other programs. 我对C ++还是很陌生,但是到了要建立自己的库以供其他程序使用的地步。 I've run into an organization issue which can be solved with namespaces and passing structs, but I'd like to organize my functions within a class. 我遇到了一个组织问题,可以使用名称空间和传递结构来解决,但是我想在一个类中组织函数。 I have searched, but have not found many clear answers. 我已经搜索过,但是没有找到很多明确的答案。 I would also like to avoid having to use "friend class x" as I have written many of the functions already and plan to write more, and all the referencing could be avoided if I kept it in the same object. 我还希望避免使用“朋友类x”,因为我已经编写了许多函数,并计划编写更多函数,并且如果将其保留在同一对象中,则可以避免所有引用。

class FileOps
private:
<data to be manipulated>
public:
    // Target Management:
    void setDir(std::string input);
    void setFile(std::string target, std::string extension);
    // File Management:
    void listFiles(std::string manualExtension);
    void setFileList();
    bool fileSeek();
    void deleteFile();
    std::string chooseFile(std::string manualExtension);
    // File Content:
    void displayFileContents();
    std::string randomLine();
    void listManagement();
    // Database Management:
    void typeList();
    void smartBuffer();
    void dataBuffer(std::string dataType);
    searchTable searchQuery;
    std::string searchSimple(std::string searchType);
    void searchDump();
    void globalDataSearch();
    void globalTermSearch();
    void globalDataDump();
    void dataDump(std::string dataTypeSelection);

I could easily fix this issue by turning the private data into a struct, then passing the struct through the arguments, but I'd like to create 1 object per class, then in the implimentation, reference the command something like: 我可以通过将私有数据转换为一个结构,然后将该结构通过参数传递来轻松解决此问题,但是我想为每个类创建1个对象,然后隐含地引用以下命令:

Fileops x;
x.TargetManage::setDir("thisdirectory");
x.FileManage::listFiles();

... etc. ...等

Is this possible? 这可能吗? If so, how? 如果是这样,怎么办?

#include <iostream>
class FileOps
public:
FileOps() {TargetManage.f = this;}
private:
friend class s1;
int x = 1;
public:
    class s1 {
    public:
        FileOps * f;
        void setDir(std::string input) {std::cout << ++f->x;}
        void setFile(std::string target, std::string extension);
    } TargetManage;
    class {
    public:
        void listFiles(std::string manualExtension);
        void setFileList();
    } FileManage;
...

Fileops x;
x.TargetManage.setDir("thisdirectory");
x.FileManage.listFiles();

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

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