Initializing static reference (non-POD) member variables

G

Grey Alien

class A
{
public:
A(const B& ref);

private:
static B& b ;
};

How may b be initialized ?
 
V

Victor Bazarov

Grey said:
class A
{
public:
A(const B& ref);

private:
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 bObj;
B& A::b = bObj;

int main()
{
}

V
 
G

Grey Alien

Victor said:
You need a static B object to initialise the reference with.

.. // definitions of B and A classes.

B bObj;
B& A::b = bObj;

int main()
{
}

V
Thanks, but what about the case where b dosesNOT have a default ctor -
and also, we need to initialize B with a SPECIFIC instance of B -
(example a database connection) - ie the instance called ref in my snippet.

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
2. Class B has no default ctor(s)
3. Class B's ctor takes a reference as one of its non-default arguments
4. The reference parameter required to construct B is provided via A's ctor

Is there a way to do this ?
 
A

Alf P. Steinbach

* Grey Alien:
Thanks, but what about the case where b dosesNOT have a default ctor -
and also, we need to initialize B with a SPECIFIC instance of B -
(example a database connection) - ie the instance called ref in my snippet.

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
2. Class B has no default ctor(s)
3. Class B's ctor takes a reference as one of its non-default arguments
4. The reference parameter required to construct B is provided via A's ctor

Is there a way to do this ?

Yes, but (1) that isn't what your code exemplifies, and (2)
initialization of non-local statics generally happens before main() is
called, and at that point you probably don't have any database
connection yet.

Why don't you explain what you're trying to achieve by using that "static"?

That solution is flawed, but if you explain what it's meant to be a
solution for, perhaps we can help with the Real Problem (TM).
 
G

Grey Alien

Alf said:
* Grey Alien:



Yes, but (1) that isn't what your code exemplifies, and (2)
initialization of non-local statics generally happens before main() is
called, and at that point you probably don't have any database
connection yet.

Why don't you explain what you're trying to achieve by using that "static"?

That solution is flawed, but if you explain what it's meant to be a
solution for, perhaps we can help with the Real Problem (TM).

1). Class A is supposed to be a 'simplistic' singleton (simply by
marking all methods/members etc static). class A represents an 'Engine',
responsible for running simulations.

2). Class B represents a specific configuration for Class A.

3). Class B contains a reference to a database connection - which class
A can also use, for CRUD operations. (A is a friend of B)
 
V

Victor Bazarov

Grey said:
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
snippet.

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.

Solve WHAT?
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
'A' itself.
2. Class B has no default ctor(s)
Irrelevant.

3. Class B's ctor takes a reference as one of its non-default
arguments
Irrelevant.

4. The reference parameter required to construct B is provided via
A's ctor

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.

V
 
V

Victor Bazarov

Alf said:
* Grey Alien:
[..]
The problem being:

1. Class A contains a static reference to Class B
2. Class B has no default ctor(s)
3. Class B's ctor takes a reference as one of its non-default
arguments 4. The reference parameter required to construct B is provided
via
A's ctor Is there a way to do this ?
Yes

Yes?

, but (1) that isn't what your code exemplifies, and (2)
initialization of non-local statics generally happens before main() is
called, and at that point you probably don't have any database
connection yet.

It's not impossible to establish a connection before 'main', so that
should not really be an issue.
Why don't you explain what you're trying to achieve by using that
"static"?

Sharing the instance of 'B' between all instances of 'A', perhaps?
That solution is flawed, but if you explain what it's meant to be a
solution for, perhaps we can help with the Real Problem (TM).

V
 
V

Victor Bazarov

Grey said:
Alf P. Steinbach wrote:
[..] but if you explain what it's meant to be a
solution for, perhaps we can help with the Real Problem (TM).

1). Class A is supposed to be a 'simplistic' singleton (simply by
marking all methods/members etc static). class A represents an
'Engine', responsible for running simulations.

Then 'A::b' does NOT have to be a static member. Make sure you
implement 'A' as a decent singleton, and neither of its members
would have to be static.
2). Class B represents a specific configuration for Class A.

3). Class B contains a reference to a database connection - which
class A can also use, for CRUD operations. (A is a friend of B)

Those are irrelevant to your problem, AIUI.

V
 
A

Alf P. Steinbach

* Victor Bazarov:
Alf said:
* Grey Alien:
[..]
The problem being:

1. Class A contains a static reference to Class B
2. Class B has no default ctor(s)
3. Class B's ctor takes a reference as one of its non-default
arguments 4. The reference parameter required to construct B is provided
via
A's ctor Is there a way to do this ?
Yes

Yes?

Yes, because the spec is so vague. You just need to think outside the
box. The box you seem to imagine is not present in the spec, which is,
for all practical purposes, useless anyway.

It's not impossible to establish a connection before 'main', so that
should not really be an issue.

In practice it's an issue. In theory you can do a lot via
initialization of non-local statics. In practice you run into
initialization order fiasco, error handling impossibilities, etc.

Sharing the instance of 'B' between all instances of 'A', perhaps?

Whatever, we don't know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top