473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Initializing static reference (non-POD) member variables

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

private:
static B& b ;
};

How may b be initialized ?
Jul 24 '07 #1
6 3203
Grey Alien wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 24 '07 #2


Victor Bazarov wrote:
Grey Alien wrote:
>>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
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 ?

Jul 24 '07 #3
Grey Alien wrote:
Victor Bazarov wrote:
>Grey Alien wrote:
>>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
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 24 '07 #4


Alf P. Steinbach wrote:
* Grey Alien:
>>

Victor Bazarov wrote:
>>Grey Alien wrote:

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

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).

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)
Jul 24 '07 #5
Alf P. Steinbach wrote:
* 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 24 '07 #6
Grey Alien wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 24 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: prabhu | last post by:
hello to all, Please can anybody tell me the differnece between static and ordinary member variables. thankyou in advance, vishnu
2
by: Drew McCormack | last post by:
I am getting an error in g++ 4.0.0 that I did not get in g++ 3.4. I have a header with the following const variables with namespace scope: namespace Periphery { extern const double...
1
by: John Ratliff | last post by:
Do I have to declare store in my implementation file for all static member variables, even when they are const ints? In Windows, using msys with g++ 3.4.2 and whatever linker I'm not sure...
10
by: Jeff Grills | last post by:
I am an experienced C++ programmer with over 12 years of development, and I think I know C++ quite well. I'm changing jobs at the moment, and I have about a month between leaving my last job and...
0
by: Adam Smith | last post by:
Are there any drawbacks to initializing static member variables for classes used in an asp.net application within application_start? My classes have arraylist like tables, which cache information...
3
by: Allen | last post by:
In our project, there is a strange problem. Please see the following codes. //////////////////////////////////////////////////////////////////////// /// SSControl.h #pragma pack(1) typedef...
15
by: Bit byte | last post by:
I am writing a small parser object. I need to store keywords etc in lsts. Because this data is to be shared by all instances of my parser class, I have declared the variable as class variables...
8
by: John | last post by:
Hello, is there any compiler option for g++ for initializing static members of the class. Due to some unknown reason, static member in one of our c++ application is not getting initialized...
13
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.