473,569 Members | 2,761 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to initialize static references to an object, second attempt

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 an instance of the class bla. According to a previous posting of
Zeppe on 4/27...
>
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.
Doing that, this is my code:

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

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

};

static bla v();

ga::ref = v;

int main()
{
ga();
return 0;
}

however, this still does not lead to any proper code, since the compiler
now complains by doing this:
test.cpp:23: error: expected constructor, destructor, or type conversion
before ‘=’ token
test.cpp:21: warning: ‘bla v()’ declared ‘static’ but never defined

what is the problem?

a. why can't I just declare bla v() as a static variable?
b. what is then the proper way of initializing ga::ref?
c. references such as the C++ annotations
(http://www.icce.rug.nl/documents/cplusplus/) or even stroustrup's book
are not that verbose on the topic of static reference members. Anyone
can point me towards some documentation on this matter?

thanks in advance,
Bram

May 2 '07 #1
4 1970
Bram Kuijper wrote:
a. why can't I just declare bla v() as a static variable?
You are declaring a function, not defining a variable. Try:
static bla v = bla(); or:
static bla v;
b. what is then the proper way of initializing ga::ref?
try:
bla & ga::ref = v;
May 2 '07 #2
On May 2, 10:35 am, Bram Kuijper <a.l.w.kuij...@ rug.nlwrote:
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 an instance of the class bla. According to a previous posting of
Zeppe on 4/27...
>
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.

Doing that, this is my code:

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

};

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

};

static bla v();
This is declaration of static function v() which returns bla.
As per standard whatever looks like declaration is a declararion.
So change it to
static bla v;
And also define constructors and destructors of bla and ga.
Your code should work now.

>
ga::ref = v;

int main()
{
ga();
return 0;

}

however, this still does not lead to any proper code, since the compiler
now complains by doing this:
test.cpp:23: error: expected constructor, destructor, or type conversion
before '=' token
test.cpp:21: warning: 'bla v()' declared 'static' but never defined

what is the problem?

a. why can't I just declare bla v() as a static variable?
b. what is then the proper way of initializing ga::ref?
c. references such as the C++ annotations
(http://www.icce.rug.nl/documents/cplusplus/) or even stroustrup's book
are not that verbose on the topic of static reference members. Anyone
can point me towards some documentation on this matter?

thanks in advance,
Bram

May 2 '07 #3
On May 2, 10:54 am, siddhu <siddharth....@ gmail.comwrote:
On May 2, 10:35 am, Bram Kuijper <a.l.w.kuij...@ rug.nlwrote:


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 an instance of the class bla. According to a previous posting of
Zeppe on 4/27...
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.
Doing that, this is my code:
class bla
{
public:
bla();
~bla();
};
class ga
{
public:
static bla& ref;
ga();
~ga();
};
static bla v();

This is declaration of static function v() which returns bla.
As per standard whatever looks like declaration is a declararion.
So change it to
static bla v;
And also define constructors and destructors of bla and ga.
Your code should work now.


ga::ref = v;
int main()
{
ga();
return 0;
}
however, this still does not lead to any proper code, since the compiler
now complains by doing this:
test.cpp:23: error: expected constructor, destructor, or type conversion
before '=' token
test.cpp:21: warning: 'bla v()' declared 'static' but never defined
what is the problem?
a. why can't I just declare bla v() as a static variable?
b. what is then the proper way of initializing ga::ref?
c. references such as the C++ annotations
(http://www.icce.rug.nl/documents/cplusplus/) or even stroustrup's book
are not that verbose on the topic of static reference members. Anyone
can point me towards some documentation on this matter?
thanks in advance,
Bram- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
also do this
bla& ga::ref = v; in place of ga::ref = v;

May 2 '07 #4
Bram Kuijper wrote:
Okay, second try (since my posting on 4/27), to find a proper way to
initialize a static reference member to an object.
[...]
a. why can't I just declare bla v() as a static variable?
the code

static bla v();

defines a static function v() that returns a object of type "bla". In
order to define a static variable with the empty constructor you have to do

static bla v;
b. what is then the proper way of initializing ga::ref?
well, it's exactly in the piece of message that you quoted:

bla& ga::ref = v;

check it out!

Regards,

Zeppe

May 2 '07 #5

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

Similar topics

9
6350
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you...
5
4551
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 know you wouldn't use 'C' { private: static std::vector<int> PSArray; public:
16
3137
by: Ed Sutton | last post by:
I use a mutex to disallow starting a second application instance. This did not work in a release build until I made it static member of my MainForm class. In a debug build, first instance got ownership, second did not and terminated. In a release build, the second instance *also* got ownership. I "fixed" it by making the mutex a static...
2
2003
by: Brano | last post by:
Hello guys, sorry for the really dumb question, but I'm just beginner in linux programming. I have the following code in test.c: #include <stdio.h> #include <libpq-fe.h> int main() { int do_init = 1; PQinitSSL(do_init); }
9
1351
by: DrConti | last post by:
Dear Python developer community, I'm quite new to Python, so perhaps my question is well known and the answer too. I need a variable alias ( what in other languages you would call "a pointer" (c) or "a reference" (perl)) I read some older mail articles and I found that the offcial position about that was that variable referencing wasn't...
1
1640
by: Ole Nielsby | last post by:
I'm puzzled at how static fields intersect with generics, as the following snippet illustrates. namespace MonkeyParty { abstract class Fruit { } class Apple : Fruit { } class Pear : Fruit { }
1
4227
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); private: unsigned int ThreadID;
14
2556
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used in severel 'underwater operations' * there is a list which stores objects of class B There are several issues I'm not sure about:
4
8349
by: Bram Kuijper | last post by:
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...
0
7697
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...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7672
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...
0
6283
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...
1
5512
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...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.