2
votes
1answer
154 views

Code re-use in C++, via multiple inheritance or composition? Or…?

I originally asked this question on StackOverflow, but I was directed here, and I think my problem is perhaps as much conceptual as technical, so here goes. If you’re defining a hierarchy of abstract ...
3
votes
1answer
198 views

How to maintain encapsulation with composition in C++?

I am designing a class Master that is composed from multiple other classes, A, Base, C and D. These four classes have absolutely no use outside of Master and are meant to split up its functionality ...
2
votes
2answers
201 views

Testing abstract class' behavior

I'm currently refactoring an existing design, which was created without TDD. There is a class hierarchy with one abstract base class and two subclasses. In the original design, these classes were ...
4
votes
1answer
289 views

Is using protected inheritance to hide (but exploit) implemented interface from the public ok?

Recently, we had a discussion about code using protected inheritance in order to hide the fact (to client code) a class inherits from a specific base class but to exploit this fact in the ...
3
votes
4answers
404 views

Initializing derived classes in the same way

I have a class Base that has several children, say A, B, C. For testing purposes I'd like to mock those derived classes by deriving from them. So MockA derives from A, MockB derives from B and so on. ...
9
votes
6answers
11k views

Why are constructors not inherited?

I am confused as to what the problems could be if a constructor was inherited from a base class. Cpp Primer Plus says, Constructors are different from other class methods in that they create ...
7
votes
2answers
481 views

How do I avoid writing lots of pass-through functions in a wrapper?

I have a class, which wraps another class of a common base type. Because the base type interface is quite large this involves writing a lot of pass-through functions. I am looking for a way to avoid ...
8
votes
5answers
2k views

Interface and Inheritance: Best of both worlds?

I 'discovered' interfaces and I started to love them. The beauty of an interface is that it is a contract, and any object that fulfills that contract can be used wherever that interface is required. ...
3
votes
1answer
328 views

Is it possible to refactor inheritance to composition when virtual methods are called inside the base class?

Let's say I have a class called Country and two subclasses called AI and Player. The Country class has a number of virtual methods to allow player-specific or AI-specific behavior. I want to make it ...
12
votes
6answers
2k views

Real-world scenarios for protected methods

Today I noticed that I basically never use protected methods in C++ code, because I rarely feel the need to call non-public methods of a parent. I do use protected in Java in the template method ...