C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

static data members access

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Alexander Mahr
Guest





PostPosted: Wed Sep 24, 2003 10:31 pm    Post subject: static data members access Reply with quote



Dear Newsgroup,

As far as I now static data members can be used to share data between
objects of a class
but they are for a reason I don't know why only accesable within the file
they are declared.

In my opinionthis access limitation makes no real sence for it is now
impossible to
use a decleration file like cclass.h within you declare the class and a file
cclass.cpp in
which you write the member functions of the class which of couse shall acces
and change
the static data member?

Can anyone tell me why
static duration comes with a
access limitation?

Regards Alexander




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ulrich Eckhardt
Guest





PostPosted: Thu Sep 25, 2003 1:38 pm    Post subject: Re: static data members access Reply with quote



Alexander Mahr wrote:
Quote:
As far as I now static data members can be used to share data
between objects of a class but they are for a reason I don't know
why only accesable within the file they are declared.

You messed up the syntax and possibly the difference between declaraion and
definition. Here's an example how to get it right:

/////////// A.h

struct A
{
// declaration
static float hoover;

void foo() const;
};

void bar();

////////// A.cpp

// definition
float A::hoover = 3.14f;

void A::foo()
{
print(hoover);
}

void bar()
{
print(A::hoover+1.0f);
}

// note: doing this is something different
static long lag;


Ulrich Eckhardt

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gerhard Menzl
Guest





PostPosted: Thu Sep 25, 2003 1:45 pm    Post subject: Re: static data members access Reply with quote



Alexander Mahr wrote:

Quote:
As far as I now static data members can be used to share data between
objects of a class but they are for a reason I don't know why only
accesable within the file they are declared.

In my opinionthis access limitation makes no real sence for it is now
impossible to use a decleration file like cclass.h within you declare
the class and a file cclass.cpp in which you write the member
functions of the class which of couse shall acces and change
the static data member?

Can anyone tell me why static duration comes with a access limitation?

There is no such limitation. Could it be that you have declared the
static member in the class definition but forgotten to define it in the
implementation file? It is hard to tell without seeing the code that
gives you troubles, so please post a stripped-down example that exhibits
the problem.

Gerhard Menzl


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Thu Sep 25, 2003 3:42 pm    Post subject: Re: static data members access Reply with quote

In article <bksbbk$upf$07$1 (AT) news (DOT) t-online.com>, Alexander Mahr
<mahralex (AT) gmx (DOT) de> writes
Quote:
Dear Newsgroup,

As far as I now static data members can be used to share data between
objects of a class
but they are for a reason I don't know why only accesable within the file
they are declared.

In my opinionthis access limitation makes no real sence for it is now
impossible to
use a decleration file like cclass.h within you declare the class and a file
cclass.cpp in
which you write the member functions of the class which of couse shall acces
and change
the static data member?

Can anyone tell me why
static duration comes with a
access limitation?


You are confusing different uses of static. If I write:

static int i;

At file scope the result will be that i has what is called internal
linkage and will not be exposed outside the file in which it is
declared. That means that if another file contains a declaration of i
(static or not) at file scope it will be a different i. The combination
of:

int i;
in one file and
extern int i;
in other files is the way to inform the compiler that all these files
are sharing the definition of i provided by the first declaration. i is
said to have external linkage and will be visible to the linker.

class mt {
static int i;
// rest of class
};

is an entirely different beast. It is a pure declaration of i as a
member of mt that belongs to the class rather than to individual
instances of mt. As a pure declaration there must be a single definition
available to the linker. This should be provided by something such as:

int mt::i = 0;

usually placed in the implementation file for class mt. It would almost
certainly be an error to write:

static int mt::i = 0;

I say almost certainly because I want to protect myself from someone
coming up with some bizarre instance where that was the right thing to
do (my creative imagination fails to produce such a case).

--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tonci Tomic
Guest





PostPosted: Thu Sep 25, 2003 8:41 pm    Post subject: Re: static data members access Reply with quote


"Alexander Mahr" <mahralex (AT) gmx (DOT) de> wrote

Quote:
Dear Newsgroup,

As far as I now static data members can be used to share data between
objects of a class
but they are for a reason I don't know why only accesable within the file
they are declared.


Wrong. Static data members are accessible from everywhere.

Quote:
In my opinionthis access limitation makes no real sence for it is now
impossible to
use a decleration file like cclass.h within you declare the class and a
file
cclass.cpp in
which you write the member functions of the class which of couse shall
acces
and change
the static data member?

Wrong premisse leads to wrong conclusion.
Can you write a little example in which you will explain what you want to
achieve and that, by your opinion, is not possible?



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daniel Spangenberg
Guest





PostPosted: Thu Sep 25, 2003 9:24 pm    Post subject: Re: static data members access Reply with quote

Hello, Alexander Mahr!

Alexander Mahr schrieb:

Quote:
Dear Newsgroup,

As far as I now static data members can be used to share data between
objects of a class
but they are for a reason I don't know why only accesable within the file
they are declared.

In my opinionthis access limitation makes no real sence for it is now
impossible to
use a decleration file like cclass.h within you declare the class and a file
cclass.cpp in
which you write the member functions of the class which of couse shall acces
and change
the static data member?

Can anyone tell me why
static duration comes with a
access limitation?

Regards Alexander

It seems that you mix up the different meanings of the keyword "static" and/or
the meaning of the member-access descriptors "public", "protected", and
"private".

Static has several meanings. The meaning of "static" for static data members can
be
understood as a "storage duration" descriptor. The standard defines static,
automatic and dynamic storage durations where static storage duration can be
loosely
translated as "existing for the duration of the program".

(Regrettably a static lifetime duration makes not much sense for functions. But
such static
member functions behave like a simple non-member function despite that its
access
is controlled by the class access rights.)

It seems that you mix up this meaning of static with static meant as linkage
descriptor,
which is the (deprecated) way to say that a named entity in namespace scope
shall have
internal linkage (Which means that it can only be accessed via its name inside
the translation
unit where it is defined)

One example is:

static const double pipapo = 123.4556;

This meaning of static here is different to that of static data members, because
static
(data) members always have external linkage and can thus be reached by their
name across
different translation units. (One exception exists: Such static class members do
not
have external linkage if the class name itself has not)

The conclusion is: Normally static data members can be accessed by name across
translation units. It depends only on the access specifier (public, private,
protected) of this very
member and the rights the caller has whether (s)he is **allowed** to use this
name.

Hope that helps,

Daniel






[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gareth Stockwell
Guest





PostPosted: Thu Sep 25, 2003 11:34 pm    Post subject: Re: static data members access Reply with quote

Alexander,

I'm not sure exactly what you're asking here, but I hope that the code
snippet below might help to show how static members can be used.

//--------------------------

// In a file x.h, declare a class X, which has a static data member,
// and a function which accesses that member by returning a reference
// to it

class X {
public:
static int& i() { return i_; }
private:
static int i_;
};


// Make sure that the member i_ is initialised somewhere - the most
// obvious place would be in the implementation file x.cpp

#include "x.h"
int X::i_ = 50;


// Now use the 'X' class - this code can be in a separate file
// e.g. main.cpp, as long as the x.o object file (which contains the
// initialisation of X::i_) is included at the linking stage

#include "x.h"
#include <iostream>

int main() {

X x1, x2; // Declare two instances of X

std::cout << "x1.i = " << x1.i() << std::endl;
std::cout << "x2.i = " << x2.i() << std::endl;

x1.i() = 99; // Access the static member X::i_ via object x1

// Hey presto, it has changed in x2 also
std::cout << "x2.i = " << x2.i() << std::endl;
}

//--------------------------



This program should print out the following

x1.i = 50
x2.i = 50
x2.i = 99

I hope that helps,
Gareth




"Alexander Mahr"
Quote:
Dear Newsgroup,

As far as I now static data members can be used to share data between
objects of a class
but they are for a reason I don't know why only accesable within the file
they are declared.

In my opinionthis access limitation makes no real sence for it is now
impossible to
use a decleration file like cclass.h within you declare the class and a file
cclass.cpp in
which you write the member functions of the class which of couse shall acces
and change
the static data member?

Can anyone tell me why
static duration comes with a
access limitation?

Regards Alexander

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.