G
Grey Alien
class A
{
public:
A(const B& ref);
private:
static B& b ;
};
How may b be initialized ?
{
public:
A(const B& ref);
private:
static B& b ;
};
How may b be initialized ?
Grey said:class A
{
public:
A(const B& ref);
private:
static B& b ;
};
How may b be initialized ?
Thanks, but what about the case where b dosesNOT have a default ctor -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 ?
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).
Grey said: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)
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
Is there a way to do this ?
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.
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).
Grey said:Alf P. Steinbach wrote:
[..] but if you explain what it's meant to be asolution 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)
Alf said:* Grey Alien:Yes[..]
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?
It's not impossible to establish a connection before 'main', so that
should not really be an issue.
Sharing the instance of 'B' between all instances of 'A', perhaps?
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.