473,372 Members | 1,237 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,372 software developers and data experts.

how to initialize static references to an object?

Hi all,

as a C++ newbie, I got some question on the initialization of static
reference data members.

Since it isn't possible to initialize static members of a class in the
constructor, I should initialize them in advance. However, the following
code, in which I first produce two classses and then try to assign a
reference of the first class to a static data member of the second class
doesn't work. It gives the following compiler error:

error: no match for operator= in ga::ref = v
note: candidates are: bla& bla::operator=(const bla&)

Since I don't wanna go into operator overloading for something this
simple, how should I properly initialize a static member referencing to
an object?

cheers,
Bram
here is my code:
class bla
{
public:
bla();
~bla();

};

class ga
{
public:
static bla &ref;
ga();
~ga();

};

int main()
{
bla v();
ga::ref = v;
return 0;
}

Apr 27 '07 #1
4 8298
Bram Kuijper wrote:
Hi all,

as a C++ newbie, I got some question on the initialization of static
reference data members.

Since it isn't possible to initialize static members of a class in the
constructor, I should initialize them in advance. However, the following
code, in which I first produce two classses and then try to assign a
reference of the first class to a static data member of the second class
doesn't work. It gives the following compiler error:

error: no match for operator= in ga::ref = v
note: candidates are: bla& bla::operator=(const bla&)

Since I don't wanna go into operator overloading for something this
simple, how should I properly initialize a static member referencing to
an object?

cheers,
Bram
here is my code:
class bla
{
public:
bla();
~bla();

};

class ga
{
public:
static bla &ref;
pay attention with static member references, because of the
initialization order. The static variables are initialized when the
program is started, and the order of initialization is very important.
ga();
~ga();

};
>
int main()
{
bla v();
ga::ref = v;
this is a quite serious error. ga::ref is created when you first run the
program, before the main is executed. Then you are only assigning v to
an unbinded reference.

you should initialize ga::ref outside of any function, like

bla& ga::ref = v;

but v should be a static variable istantiated before in the same
transactional unit, or the result of a function.

If you want a shared object that you can change over time, you may want
to use a pointer instead of a reference.

Regards,

Zeppe

return 0;
}
Apr 27 '07 #2
On 428, 12ʱ56, Bram Kuijper <a.l.w.kuij....@rug.nlwrote:
Hi all,

as a C++ newbie, I got some question on the initialization of static
reference data members.

Since it isn't possible to initialize static members of a class in the
constructor, I should initialize them in advance. However, the following
code, in which I first produce two classses and then try to assign a
reference of the first class to a static data member of the second class
doesn't work. It gives the following compiler error:

error: no match for 'operator=' in 'ga::ref = v'
note: candidates are: bla& bla::operator=(const bla&)

Since I don't wanna go into operator overloading for something this
simple, how should I properly initialize a static member referencing to
an object?

cheers,
Bram

here is my code:

class bla
{
public:
bla();
~bla();

};

class ga
{
public:
static bla &ref;
ga();
~ga();

};

int main()
{
bla v();
ga::ref = v;
return 0;}
class bla
{
public:
bla();
~bla();

};
class ga
{
public:
static bla &ref;
ga();
~ga();
};
/////////////////////////////////add this line
bla ga::&ref;

int main()
{
bla v();
ga::ref = v;
return 0;

}

Apr 27 '07 #3
SimpleCode wrote:
/////////////////////////////////add this line
bla ga::&ref;
to add another error:

../testtraing.cpp:21: error: expected unqualified-id before & token
../testtraing.cpp: In function int main():
../testtraing.cpp:26: error: no match for operator= in ga::ref = v
../testtraing.cpp:4: note: candidates are: bla& bla::operator=(const bla&)

fortunately id doesn't compile, because it would have been wrong, as I
explained.

Regards,

Zeppe
Apr 27 '07 #4
On 428, 1ʱ34, Zeppe <zeppe.remove.all.this.long.comm...@email.it>
wrote:
SimpleCode wrote:
/////////////////////////////////add this line
bla ga::&ref;

to add another error:

./testtraing.cpp:21: error: expected unqualified-id before '&' token
./testtraing.cpp: In function 'int main()':
./testtraing.cpp:26: error: no match for 'operator=' in 'ga::ref = v'
./testtraing.cpp:4: note: candidates are: bla& bla::operator=(const bla&)

fortunately id doesn't compile, because it would have been wrong, as I
explained.

Regards,

Zeppe
Thanks Zeppe.
I got a serious mistake.It shoud "bla & ga::ref;"
But it is another mistake.
As you said,
"
this is a quite serious error. ga::ref is created when you first run
the
program, before the main is executed. Then you are only assigning v
to
an unbinded reference.
but v should be a static variable istantiated before in the same
transactional unit, or the result of a function.
"
Thank you.

Apr 27 '07 #5

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

Similar topics

2
by: Seb | last post by:
I am trying to initialize a const static object data member in a header file? The following code errs. class Object { public: virtual const char* ToString() { return "Object"; } virtual...
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
5
by: Jim Langston | last post by:
What I want to do: have a vector of ints in my class initialized with 0 to 499 which will later be pushed/popped out of the vector by instances. What I have: class CParticleStream // Yes, I...
5
by: | last post by:
Hi, How long do webservice objects live for? In particular, if i have static variables filled with data from a static constructor in a webservice, how long will that data persist? thxs
4
by: bob | last post by:
Why doesn't c++ let me initialize static members like this: class MySound { static CSoundManager* g_pSoundManager = NULL; };
1
by: philwozza | last post by:
Hi I have a THREAD class that uses the static variable NextThreadID to store the id of the next thread to be created and a static Mutex to protect it. class THREAD { public: int Start(void);...
1
by: Vol | last post by:
Hi, Group, I want to initialize a 2D static array in function 'foo ()', where 'foo' is called by another function a lot of times. I list my implementation below but I think there are better...
4
by: Bram Kuijper | last post by:
Okay, second try (since my posting on 4/27), to find a proper way to initialize a static reference member to an object. I try to initialize a static reference inside the class ga, referencing to...
18
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.