469,924 Members | 1,421 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,924 developers. It's quick & easy.

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 1769
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Bryan Parkoff | last post: by
5 posts views Thread by Jim Langston | last post: by
2 posts views Thread by Brano | last post: by
9 posts views Thread by DrConti | last post: by
1 post views Thread by Ole Nielsby | last post: by
1 post views Thread by philwozza | last post: by
14 posts views Thread by Jeroen | last post: by
4 posts views Thread by Bram Kuijper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.