473,757 Members | 10,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3247
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
2768
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
3589
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 ProtonMassInAtomicUnits = 1836.152755656068; } I try to use these in another header to initialize static const member
1
2130
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 (probably gnu binutils), I didn't have to declare storage for any of my static const integer member variables, but in Linux, the linker can't find four of them. I'm assuming the ones it found were simply replaced by the compiler and it didn't need to...
10
1870
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 starting my new one. In that time, I have decided to learn C#. I picked up the book, "Programming C# (4th Edition)" recently and have read most of it. I've written about 1,500 lines of C# now, and I've run into the first really ugly thing I...
0
1115
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 so that I don't have to hit the database every call. I would like to initialize these tables through calling a static class function, Init(), in application start. Thanks.
3
2356
by: Allen | last post by:
In our project, there is a strange problem. Please see the following codes. //////////////////////////////////////////////////////////////////////// /// SSControl.h #pragma pack(1) typedef struct { ... } Origin;
15
3565
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 (i.e. statics). //declarations in parser class (private section) static list<stringm_keywords, m_symbols_used; static map<string, myParser::FuncDatam_mapped_funcs ; I have initialization code like this:
8
2772
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 properly. Please help me on this. Thanks,
13
11472
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
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.