C++, removing #include or #include in class header

后端 未结 12 1439
感情败类
感情败类 2021-01-03 06:37

I want to remove, if possible, the includes of both and from my class header file. Both string and vector are return types of functions declare

12条回答
  •  梦毁少年i
    2021-01-03 06:58

    If string and vector are used only in signatures of non-public members of you class, you could use the PImpl idiom:

    // MyClass.h
    class MyClassImpl;
    class MyClass{
    public:
        MyClass();
        void MyMethod();
    private:
        MyClassImpl* m_impl;
    };
    
    // MyClassImpl.h
    #include 
    #include 
    #include 
    class MyClassImpl{
    public:
        MyClassImpl();
        void MyMethod();
    protected:
        std::vector StdMethod();
    };
    
    // MyClass.cpp
    #include 
    #include 
    
    void MyClass::MyMethod(){
        m_impl->MyMethod();
    }
    

    You are always including vector and string in the header file, but only in the implementation part of your class; files including only MyClass.h will not be pulling in string and vector.

提交回复
热议问题