简体   繁体   中英

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. 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.

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:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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