Connecting Tech Pros Worldwide Help | Site Map

How to define and initialize the protected static members ?

  #1  
Old November 21st, 2008, 08:25 PM
Timothy Madden
Guest
 
Posts: n/a
Hy

static members of non-integral type need to be declared in the class,
but defined (and constructed or initialized) outside the class.

Like this

class SystemName
{
public:
std::string sys_name;
SystemName(std::string const &sys_name)
: sys_name(sys_name)
{
}
protected:
static std::string system_type;
};

Now in a .cc file I want to define and initialize
SystemName::system_type like this

std::string SystemName::system_type("basicplatform");

but my compiler (g++ 3.4.5) sys
"'System::system_type' is protected in this context".

How am I supposed to initialize it ?

Thank you,
Timothy Madden
  #2  
Old November 21st, 2008, 08:45 PM
Victor Bazarov
Guest
 
Posts: n/a

re: How to define and initialize the protected static members ?


Timothy Madden wrote:
Quote:
Hy
Hy yourself.
Quote:
static members of non-integral type need to be declared in the class,
but defined (and constructed or initialized) outside the class.
>
Like this
>
class SystemName
{
public:
std::string sys_name;
SystemName(std::string const &sys_name)
: sys_name(sys_name)
{
}
protected:
static std::string system_type;
};
>
Now in a .cc file I want to define and initialize
SystemName::system_type like this
>
std::string SystemName::system_type("basicplatform");
>
but my compiler (g++ 3.4.5) sys
"'System::system_type' is protected in this context".
So? Is that an error? Where in the file do you define it? I do hope
it's not in a function.
Quote:
How am I supposed to initialize it ?
Just like you did. The compiler has no reason to complain. Static
members need to be defined. Their access specifiers have no bearing on
the definition/initialisation.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
  #3  
Old November 21st, 2008, 10:15 PM
blargg
Guest
 
Posts: n/a

re: How to define and initialize the protected static members ?


Timothy Madden wrote:
Quote:
static members of non-integral type need to be declared in the class,
but defined (and constructed or initialized) outside the class.
>
Like this
>
class SystemName
{
public:
std::string sys_name;
SystemName(std::string const &sys_name)
: sys_name(sys_name)
{
}
protected:
static std::string system_type;
};
>
Now in a .cc file I want to define and initialize
SystemName::system_type like this
>
std::string SystemName::system_type("basicplatform");
>
but my compiler (g++ 3.4.5) sys
"'System::system_type' is protected in this context".
>
How am I supposed to initialize it ?
First, give us a minimal test program. How does your compiler respond to
the following program?

class Foo {
protected:
static int i;
};

int Foo::i( 123 );

int main() { }

By taking the time to write a minimal test program, in almost all cases
you'll track down the real cause of the bug, and its solution. In other
words, learn to fish.
  #4  
Old November 21st, 2008, 11:45 PM
Timothy Madden
Guest
 
Posts: n/a

re: How to define and initialize the protected static members ?


blargg wrote:
Quote:
Timothy Madden wrote:
Quote:
>static members of non-integral type need to be declared in the class,
>but defined (and constructed or initialized) outside the class.
>>
>Like this
>>
>class SystemName
>{
>public:
> std::string sys_name;
> SystemName(std::string const &sys_name)
> : sys_name(sys_name)
> {
> }
>protected:
> static std::string system_type;
>};
>>
>Now in a .cc file I want to define and initialize
>SystemName::system_type like this
>>
>std::string SystemName::system_type("basicplatform");
>>
>but my compiler (g++ 3.4.5) sys
>"'System::system_type' is protected in this context".
>>
>How am I supposed to initialize it ?
>
First, give us a minimal test program. How does your compiler respond to
the following program?
>
class Foo {
protected:
static int i;
};
>
int Foo::i( 123 );
>
int main() { }
>
By taking the time to write a minimal test program, in almost all cases
you'll track down the real cause of the bug, and its solution. In other
words, learn to fish.
Well you minimal test program compiles fine.
My error was on a line further down. My code is:

using namespace std;

extern
class SystemName
{
public:
string sys_name;
protected:
SystemName(string sys_name)
: sys_name(sys_name)
{
}
static SystemName sys_type;
}
&systype;

SystemName SystemName::sys_type("basicplatform");

SystemName &systype = SystemName::sys_type;

I am trying to write a single-ton class, that can only be accessed
through a reference, SystemName &systype, instead of a function
like SystemName &SystemName::getInstance(), which is ugly.

The real code is at work and is for a class that exposes
application-global settings as public data members initialized
from the application's configuration file. I think a single-ton
class is appropriate for such a case, and I want the users of
the configuration class to be able to access just the reference.

The error is obviously on the last line where the reference is
initialized with the protected member, but still in the compiler
output messages the first error is listed at the previous line,
with the definition of the static member, making me believe the
compiler could not define the protected static member.

I do not know why g++ reports the error as if on a previous line,
even in version 4.2.4

Thank you,
Timothy Madden


/*
* POSIX version test
*
* Program to test POSIX version, if running on a POSIX system,
* at compile and run time, and display values found. Returns
* 0 to indicate success if on a POSIX system, non-zero otherwise.
*/

#define _POSIX_SOURCE 199309

#include <cstdlib>
#include <iostream>
#include <unistd.h>

#define STRINGIZE(a) #a
#define MAKE_STRING2(a) STRINGIZE(a)
#define MAKE_STRING(a) MAKE_STRING2(a)

using namespace std;

extern
class SystemName
{
public:
string sys_name;
protected:
SystemName(string sys_name)
: sys_name(sys_name)
{
}
static SystemName sys_type;
}
&systype;

SystemName SystemName::sys_type("basicplatform");

SystemName &systype = SystemName::sys_type;


int main()
try
{
#ifdef _POSIX_VERSION
cout << "Built for POSIX ";
cout << MAKE_STRING(_POSIX_VERSION);
cout << endl;

if (sysconf(_SC_VERSION) != -1)
{
cout << "Running on POSIX ";
cout << sysconf(_SC_VERSION);
cout << endl;

return EXIT_SUCCESS;
}
else
{
cout << "Not running on a POSIX system.\n";

return EXIT_FAILURE;
}
#else
cout << "Not built on a POSIX system.\n";
return EXIT_FAILURE;
#endif
}
catch (exception &e)
{
cerr << e.what();
cerr << endl;
return EXIT_FAILURE;
}
catch (...)
{
cerr << "Application error.\n";
return EXIT_FAILURE;
}


  #5  
Old November 22nd, 2008, 12:05 AM
blargg
Guest
 
Posts: n/a

re: How to define and initialize the protected static members ?


Timothy Madden <terminatorul@gmail.comwrote:
Quote:
blargg wrote:
[...]
Quote:
My error was on a line further down. My code is:
>
using namespace std;
>
extern
class SystemName
{
public:
string sys_name;
protected:
SystemName(string sys_name)
: sys_name(sys_name)
{
}
static SystemName sys_type;
}
&systype;
>
SystemName SystemName::sys_type("basicplatform");
>
SystemName &systype = SystemName::sys_type;
>
I am trying to write a single-ton class, that can only be accessed
through a reference, SystemName &systype, instead of a function
like SystemName &SystemName::getInstance(), which is ugly.
>
The real code is at work and is for a class that exposes
application-global settings as public data members initialized
from the application's configuration file. I think a single-ton
class is appropriate for such a case, and I want the users of
the configuration class to be able to access just the reference.
>
The error is obviously on the last line where the reference is
initialized with the protected member, but still in the compiler
output messages the first error is listed at the previous line,
with the definition of the static member, making me believe the
compiler could not define the protected static member.
>
I do not know why g++ reports the error as if on a previous line,
even in version 4.2.4
I just tried (with the addition of #include <string>) and sure enough, I
get this error:

test.cpp:17: error: "SystemName SystemName::sys_type" is protected
test.cpp:19: error: within this context

Line 17 is the definition of sys_type, and line 19 is where you try to
make a reference to it. The compiler could have given us the context
within the class definition, rather than the definition, but it's clear
enough to find the error. If I remove the definition of sys_type, it gives
a better context reference to the "static SystemName sys_type" line in the
class definition:

test.cpp:13: error: "SystemName SystemName::sys_type" is protected
test.cpp:19: error: within this context

Objects/references to SystemName don't get special access to
protected/private members in their initializers, so you'll have to find
another way to expose sys_type. BTW, practice writing minimal test code,
as it will greatly increase the chances of people here reading your post
and spending time solving it. The following is a better test case for the
problem you had:

class Foo {
protected:
static Foo instance;
};

Foo Foo::instance;

Foo& foo = Foo::instance;

int main() { }
/*
$ g++ test.cpp
test.cpp:6: error: 'Foo Foo::foo' is protected
test.cpp:8: error: within this context
*/
  #6  
Old November 22nd, 2008, 12:15 AM
blargg
Guest
 
Posts: n/a

re: How to define and initialize the protected static members ?


blargg wrote:
[...]
Quote:
The following is a better test case for the problem you had:
>
class Foo {
protected:
static Foo instance;
};
>
Foo Foo::instance;
>
Foo& foo = Foo::instance;
>
int main() { }
/*
$ g++ test.cpp
test.cpp:6: error: 'Foo Foo::foo' is protected
test.cpp:8: error: within this context
*/
....except for the fact that I stupidly changed the source code without
regenerating the error message, which should be:

$ g++ test.cpp
test.cpp:6: error: 'Foo Foo::instance' is protected
test.cpp:8: error: within this context
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
A quick introduction to Java concepts 1 r035198x insights 3 April 5th, 2008 08:56 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 07:46 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 03:55 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 5 November 14th, 2005 12:36 PM