C++ class definition split into two headers?

*爱你&永不变心* 提交于 2019-12-02 11:39:27

问题


Is it possible in C++ to split the definition of class members in two headers? What would be the appropriate way to code it?

For instance:

a1.h

class A {
    public:
        int var;
        void foo1(int b);
}

a1.cpp

#include "a1.h"

void A::foo1(int b) {
    cout << b;
}

a2.h

[extend] class A {
    public:
        void foo2(double c);
}

a2.cpp

#include "a2.h"

void A::foo2(double c) {
    cout << c;
}

回答1:


You can't extend a class that way, but you can use the pimpl pattern:

class A {
public:
    void foo1(int b);
private:
    AImpl* pimpl;

}

and then have AImpl.h and AImpl.cpp that hides all the private details.



来源:https://stackoverflow.com/questions/24388051/c-class-definition-split-into-two-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!