Note that there are some explanatory texts on larger screens.

plurals
  1. POSeparation of interface from implementation in an inheritance hierarchy (C++ newbie)
    text
    copied!<p>I am trying to figure out how to arrange some classes. This is what I've got so far ...</p> <p>The top of the inheritance hierarchy is (naturally) T:</p> <pre><code>(T.h) namespace foo { class T { public: virtual void method1(std::string a_parameter) = 0; virtual void method2() = 0; }; } </code></pre> <p>I have two sub-classes of T with some additional methods - here are the header files:</p> <pre><code>(A.h) namespace foo { class A : public T { public: virtual ~A() {}; virtual void method3() = 0; //and a factory function static A* gimmeAnAyy(); }; } (B.h) namespace foo { class B : public T { public: virtual ~B() {}; virtual void method4() = 0; //and a factory function static B* gimmeABee(); }; } </code></pre> <p>The implementation classes are in the respective .cpp files:</p> <pre><code>(A.cpp) namespace foo { class AImpl : public A { public: A(std::string member_data) : m_member_data(member_data) {}; ~A() {}; void method3() { //something really important, can't think of it right now ;-) }; private: std::string m_member_data; }; A* A::gimmeAnAyy() { return new AImpl("this is an impl of A"); }; } (B.cpp) namespace foo { class BImpl : public B { public: B(std::string other_data) : m_other_data(other_data) {}; ~B() {}; void method4() { //something really important, can't think of it right now ;-) }; private: std::string m_other_data; }; B* B::gimmeABee() { return new BImpl("this is an imll of B"); }; } </code></pre> <p>Now the compiler complains - rightly so - about the virtual functions method1() and method2() that I haven't implemented in AImpl and BImpl.</p> <p>What I want is a TImpl class that both AImpl and BImpl can inherit from so that I don't have to implement method1() and method2() in two different .cpp files.</p> <p>Is it possible? Am I out to lunch? Am I asking too many rhetorical questions for a StackExchange post?</p> <h2>Thanks in advance,</h2> <p>Mike</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload