I have the following C++-class:
// Header-File
class A
{
public:
A();
private:
B m_B;
C m_C;
};
// cpp-File
A::A()
: m_B(1)
{
m_B.doSom
The pointer sounds like the only clean solution to me. The only other solution I see is to have a default constructor for C that does nothing and have an initialising method in C you call yourself later.
m_C.Initialise( m_B.getSomeValue() );
Easiest is storing pointers to a B
and a C
. These can be initialized to 0, omitting any construction. Be careful not to dereference a null pointer and delete it in the destructor of A
(or use std::unique_ptr
/boost::scoped_ptr
).
But why not initialize m_B
first (through a proper constructor call, not in A::A()
, and then use that initialized B
instance to initialize m_C
? It will call for a small rewrite, but I bet it'll be worth the code cleanup.
What you ask is forbidden - and correctly so. This ensures that every member is correctly initialized. Do not try to work around it - try to structure your classes that they work with it.
Idea: