473,721 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to define and initialize the protected static members ?

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_na me)
{
}
protected:
static std::string system_type;
};

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

std::string SystemName::sys tem_type("basic platform");

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

How am I supposed to initialize it ?

Thank you,
Timothy Madden
Nov 21 '08 #1
5 7236
Timothy Madden wrote:
Hy
Hy yourself.
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_na me)
{
}
protected:
static std::string system_type;
};

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

std::string SystemName::sys tem_type("basic platform");

but my compiler (g++ 3.4.5) sys
"'System::syste m_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.
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
Nov 21 '08 #2
Timothy Madden wrote:
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_na me)
{
}
protected:
static std::string system_type;
};

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

std::string SystemName::sys tem_type("basic platform");

but my compiler (g++ 3.4.5) sys
"'System::syste m_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.
Nov 21 '08 #3
blargg wrote:
Timothy Madden wrote:
>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_na me)
{
}
protected:
static std::string system_type;
};

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

std::string SystemName::sys tem_type("basic platform");

but my compiler (g++ 3.4.5) sys
"'System::syst em_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(stri ng sys_name)
: sys_name(sys_na me)
{
}
static SystemName sys_type;
}
&systype;

SystemName SystemName::sys _type("basicpla tform");

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::ge tInstance(), 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(stri ng sys_name)
: sys_name(sys_na me)
{
}
static SystemName sys_type;
}
&systype;

SystemName SystemName::sys _type("basicpla tform");

SystemName &systype = SystemName::sys _type;
int main()
try
{
#ifdef _POSIX_VERSION
cout << "Built for POSIX ";
cout << MAKE_STRING(_PO SIX_VERSION);
cout << endl;

if (sysconf(_SC_VE RSION) != -1)
{
cout << "Running on POSIX ";
cout << sysconf(_SC_VER SION);
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 << "Applicatio n error.\n";
return EXIT_FAILURE;
}
Nov 21 '08 #4
Timothy Madden <te**********@g mail.comwrote:
blargg wrote:
[...]
My error was on a line further down. My code is:

using namespace std;

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

SystemName SystemName::sys _type("basicpla tform");

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::ge tInstance(), 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
*/
Nov 22 '08 #5
blargg wrote:
[...]
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
Nov 22 '08 #6

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

Similar topics

9
3149
by: Bob Rock | last post by:
Hello, I was wondering when should static constructors be defined or are they even required??? Also, when are they implicitly called??? Bob Rock
2
550
by: Seb | last post by:
I am trying to initialize a const static object data member in a header file? The following code errs. class Object { public: virtual const char* ToString() { return "Object"; } virtual DataType GetType() { return DataType( "Object" ); } protected: const static DataType _dataType( "Object");
4
6159
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is, its name can be used only by members and friends of the class in which it is
13
7731
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. They don't actually protect any encapsulation or anything, and for all the actual protection they offer, they might as well be public. For example: class B { protected:
11
3838
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
9
27383
by: subramanian | last post by:
I am a beginner in C++. Suppose I want to build a class: I have given below the starting code: class Date { int day, month, year; static Date default_date; };
16
3628
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
4
8361
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 class to a static data member of the second class doesn't work. It gives the following compiler error:
9
3412
by: Steven Woody | last post by:
Hi, Supposing a class get a complicated static member foo, and it need to be initialized before any method of the class can be called, where should I put these initialization code? I don't want to put them in main(), it's so far away. Thanks. -
0
8736
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9373
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9227
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9145
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9081
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5992
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4761
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3206
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2590
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.