Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ class definition split into two headers?

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;
}
like image 376
Davinish Avatar asked Dec 02 '25 02:12

Davinish


1 Answers

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.

like image 122
Paul Evans Avatar answered Dec 03 '25 17:12

Paul Evans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!