473,773 Members | 2,334 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 7240
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
3153
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
6165
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
7735
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
3844
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
27384
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
3631
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
8368
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
3418
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
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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
10264
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...
1
10039
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
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6717
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
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4012
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
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.