Re: Initializing static reference (non-POD) member variables
Grey Alien wrote:
Victor Bazarov wrote:
Grey Alien wrote:
A(const B& ref);
static B& b ;
How may b be initialized ?
You need a static B object to initialise the reference with.
.. // definitions of B and A classes.
B& A::b = bObj;
Thanks, but what about the case where b dosesNOT have a default ctor -
The issue of initialising the 'B' object referenced by the 'A::b' was
not under discussion, was it? And it really nas nothing to do with
initialising a reference to it.
If 'B' doesn't have a default c-tor, initialise it using the c-tor
that it does have.
and also, we need to initialize B with a SPECIFIC instance of B -
(example a database connection) - ie the instance called ref in my
There is no *instance* called 'ref' in your snippet. There is the
argument of the 'A's constructor called 'ref', but it has no relation
to initialising the static member of 'A'.
An obvious way round this would be to use pointers rather than
reference types - but I just wanted to know whether there was a way
to solve this problem, using reference types instead of pointers.
The problem being:
1. Class A contains a static reference to Class B
Class 'A' contains a static data member that is a reference to
[an instance of] 'B'. You need to understand that it's a class-wide
object and it has *nothing* to do with initialising an instance of
2. Class B has no default ctor(s)
3. Class B's ctor takes a reference as one of its non-default
4. The reference parameter required to construct B is provided via
Nonsense. Static data members of a class are initialised regardless
of the semantics of initialising an *instance* of the class. No
c-tors of 'A' play any role in initialising a static member of 'A'.
Is there a way to do this ?
No, there is no way to do this. Drop the 'static' in the declaration
of 'A::b'. Make it non-static member.
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask