In C++ it's common to see things such as:
Header of class C
//== C.h ==//
#pragma once
#include "B.h"
class C
{
B b;
};
Header of class B
//== B.h ==//
#pragma once
#include "A.h"
class B
{
A a;
};
Header of class A
//== A.h ==//
#pragma once
class A
{
};
Because C.h
includes B.h
, and B.h
includes A.h
; C.h
ends up knowing implementation details about A.h
. In this case the headers are included only three levels deep, but in large projects including one header can quickly lead to the inclusion of hundreds of additional headers. Also resulting in long compile times.
In C++ 11 I could declare the headers as following:
Header of class C:
//== C.h ==//
#pragma once
#include <memory>
// proto
class B;
class C
{
std::unique_ptr<B> b;
};
Header of class B
//== B.h ==//
#pragma once
#include <memory>
// proto
class A;
class B
{
std::unique_ptr<A> a;
};
Header of class A
//== A.h ==//
#pragma once
class A
{
};
When replacing all members (class, struct) with smart pointers, I can include C.h
without having to know the implementation of class B. Every cpp file now only has to know about it's members, without having to know about the memory layout of it's members.
My question would be:
- Is it good design practice (a good idea) to replace all class members, that are either class and struct, with unique pointers? Are their additional pro's or cons ?