Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > private static member variable
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 18th November 17:13
david
External User
 
Posts: 1
Default private static member variable



Hi there,

When I try and access a static member variable within its class definition,
my compiler gives the error: Undefined reference to StaffMember::NextSpace.

I've done a bit of googling, and the consensus seems to be that if I
initialize the static variable outside of the class then this problem will
go away. But in my case NextSpace is a private data member...so what do I
do?

class StaffMember : public KeyedItem {
public:

StaffMember();
StaffMember( KeyType number, int years, int rank);
void SetStaffInfo();
KeyType GetKey ();
int GetYears ();
int GetRank ();
int GetParkSpace ();
void SetParkSpace ();
private:
int Years;
int Rank;
int ParkSpace;
static int NextSpace; // Next available parking space

Thanks!
  Reply With Quote


 


2 18th November 17:13
david
External User
 
Posts: 1
Default private static member variable



Nevermind,

I added int StaffMember::NextSpace = FIRSTPARK; to the definitions files and
all is well...
  Reply With Quote
3 18th November 17:14
chris \( val \)
External User
 
Posts: 1
Default private static member variable


| "David" <auto90059@hushmail.com> wrote in message | news:3f782e16$0$10355$afc38c87@news.optusnet.com.a u...
| > Hi there,
| >
| > When I try and access a static member variable within its class | definition,
| > my compiler gives the error: Undefined reference to | StaffMember::NextSpace.
| >
| > I've done a bit of googling, and the consensus seems to be that if I
| > initialize the static variable outside of the class then this problem will
| > go away. But in my case NextSpace is a private data member...so what do I
| > do?

[snip]

Correct!

class Base
{
private:
static int NextSpace;
};

int Base::NextSpace( 100 );

Cheers.
Chris Val
  Reply With Quote
4 18th November 17:14
jonathan mcdougall
External User
 
Posts: 1
Default private static member variable


definition,

StaffMember::NextSpace.

Static data must be defined, as functions. For functions, it is simple :
you
can define it in the class

class A
{
void f()
{
//
}
};

or outside

class A
{
void f();
}

void A::f()
{
//
}

It is the same with static data :

class A
{
static int i = 10;
};

or

class A
{
static int i;
};

int A::i = 10;


The problem with inclass static data definition is that it works only
for integral types, that is,

class B
{
static float f = 1.0; // wrong
}

is illegal. This should be possible in a future standard. Right now,
you must initialize it outside the class.

But why a different syntax for static data and non-static data? The
thing is, non-static objects are defined and initialized in a constructor,
which solves the problem. Static variables are not bound to an object so
they might not be initialized (or even defined!) if you try to access them,
so you must at least define static variables. You can use the definition
to initialize it at the same time.

Which is rare, thank you.


Is there a difference defining public and private functions outside a class?
No, and it is the same with static data.


Jonathan
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Development, Ada, Apple script, Assembler, Awk, Beos, Basic, C, C++, C#, C# .net, .net, .net frameworks, Asp .net, Clarion, Clipper, Clos, Clu, Cobol, Coldfusion, Delphi, Dylan, Eiffel, Forth, Fortran, Haskell, Hermes, Icon, Idl, Java, Java script, Jscript .net, Jcl, Linoleum, Lisp, Lotus, Limbo, Logo, Ml, Mumps, Oberon, Postscript, Pop, Pl1, Prolog, Python, Ruby, Pascal, Perl, Php, Rebol, Rexx, Sed, Sather, Scheme, Smalltalk, Tcl, Vhdl, Vrml, Visual basic, Visual basic .net, Yorick, Mysql, Omnis, Postgresql, Xbase, Access, Oracle, Adabas, Berkeley, Btrieve, Filemaker, Gupta, Db2, Informix, Ingres, Mssql server, Object, Olap, Paradox, Rdb, Revelation, Sybase, Theory, Dbase, Html, Java script, Css, Flash, Photoshop, Corel script, Xml, Tech, Beos, Gem, Hp48, Hpux, Linux, Mac, Ms-dos, Os2, Palm, Solaris, Ti99, Windows, Xenix, Aos, Chorus, Geos, Inferno, Lantastic, Lynx, Mach, Minix, Netware, Os9, Parix, Plan9, Psos, Qnx, Xinu, Sco, Unix, Aix, Aux, 386bsd, Bsdi, Freebsd, Netbsd, Openbsd, Ultrix, Amd, Intel, Aptiva, Buz, Deals, Homebuilt, Overclocking, Programming, Extra forums


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666