What you really need is a default ctor for MockHouse. If the base
class House has a default ctor, the compiler can generator a default
ctor for MockHouse and you are all set. Otherwise you need to define
the default ctor yourself.
Therefore, you can define a default ctor either for House or for
MockHouse. Either way, MockHouse will get a default ctor.
>
> -Michael
>
--
Zhanyong
If that's your choice, House's default ctor should take care of
initializing the reference member with some good default value.
>
> For instance:
>
> class House
> {
> public:
> House( DB_Connection & db_connection );
> House( );
>
> private:
> DB_Connection & m_db_connection;
> }
>
> House::House( DB_Connection & db_connection ) : m_db_connection
> ( db_connection )
> {
> }
>
> House::House( )
> {
> }
>
> This second constructor will not compile since the m_db_connection
> data member is not being initialized.
>
> I suppose I could solve this by making m_db_connection a pointer and
> then having the default constructor initialize the pointer to NULL -
> but now it seems like I'm making changes to my production code simply
> for the sake of making the mock easier to build.
There's nothing wrong with changing your production code simply for
the sake of making it more testable. Testability should be treated as
a first-class citizen.
>
> Thoughts?
>
> Thanks!
> -Michael
>
--
Zhanyong
Ah-ha. I'll give it a shot. Thanks for the response!
-Michael