473,786 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

local class problem

Hi,

I have the following code and i get a compilation error,

int main()
{
class Locale
{
public:
static int c;
};
return(0);
}

saying it is "illegal to have a static member in a local class", what
is the reason? What if i want to use the variable only inside the
function?

Thanks in advance ! ! !
Jun 27 '08 #1
8 2272
Rahul wrote:
Hi,

I have the following code and i get a compilation error,

int main()
{
class Locale
{
public:
static int c;
};
return(0);
}

saying it is "illegal to have a static member in a local class", what
is the reason? What if i want to use the variable only inside the
function?
Without checking the standard,
I guess that local class has no linkage,
while static member needs a linkage.
they just conflict.

But I don't will this be changed in the coming standard.
as local class can be used as template parameter.
(see http://www.open-std.org/jtc1/sc22/wg...2007/n2402.pdf)
Jun 27 '08 #2
On Apr 13, 7:09 pm, Barry <dhb2...@gmail. comwrote:
Rahul wrote:
Hi,
I have the following code and i get a compilation error,
int main()
{
class Locale
{
public:
static int c;
};
return(0);
}
saying it is "illegal to have a static member in a local class", what
is the reason? What if i want to use the variable only inside the
function?

Without checking the standard,
I guess that local class has no linkage,
while static member needs a linkage.
they just conflict.

But I don't will this be changed in the coming standard.
as local class can be used as template parameter.
(seehttp://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2402.pdf)
Like Barry said Member functions
of a local class have no linkage,where as static objects have internal
linkage
which conflicts and as a result you get a compiler error.
to solve the issue just try declaring a static int outside
the local class for whatever u are trying to do achieve with it.
Jun 27 '08 #3
On Apr 13, 2:16 pm, Looney <hardy_melbou.. .@hotmail.comwr ote:
On Apr 13, 7:09 pm, Barry <dhb2...@gmail. comwrote:
Rahul wrote:
Hi,
I have the following code and i get a compilation error,
int main()
{
class Locale
{
public:
static int c;
};
return(0);
}
saying it is "illegal to have a static member in a local class", what
is the reason? What if i want to use the variable only inside the
function?
Without checking the standard,
I guess that local class has no linkage,
while static member needs a linkage.
they just conflict.
But I don't will this be changed in the coming standard.
as local class can be used as template parameter.
(seehttp://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2402.pdf)

Like Barry said Member functions
of a local class have no linkage,where as static objects have internal
linkage
which conflicts and as a result you get a compiler error.
to solve the issue just try declaring a static int outside
the local class for whatever u are trying to do achieve with it.
Fine, but i guess static member variables have external linkage (as
long as a header file is included in the other files), its just like
global variables without polluting the global namespace.

Second, i'm able to have static member functions in the local
class...
Jun 27 '08 #4
Rahul wrote:
Second, i'm able to have static member functions in the local
class...
I don't think you are unless they are inline (implementing the
function inside the class declaration makes it implicitly inline).

I don't think it's possible to make static member variables "inline"
in any way, in the same sense as static member functions.

The usual way to have "local" classes is to put them inside a nameless
namespace. They will then be local to the current compilation unit,
which is usually enough.
Jun 27 '08 #5
On 13 avr, 11:09, Barry <dhb2...@gmail. comwrote:
Rahul wrote:
I have the following code and i get a compilation error,
int main()
{
class Locale
{
public:
static int c;
};
return(0);
}
saying it is "illegal to have a static member in a local
class", what is the reason? What if i want to use the
variable only inside the function?
Without checking the standard, I guess that local class has no
linkage, while static member needs a linkage. they just
conflict.
More or less. A local class doesn't have linkage, so it cannot
be named elsewhere, and everything in it must be defined in the
class itself. The declaration of a static variable inside a
class is not a definition; it requires a definition elsewhere.
But I don't will this be changed in the coming standard.
as local class can be used as template parameter.
(seehttp://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2402.pdf)
Somehow, I doubt it. I've not read the paper through in detail,
but it starts out with "all local types and unnamed types have a
`name for linkage purposes'" that is created by the compiler."
This sounds very much like the name of an anonymous namespace,
which can't be named by the programmer. If this is the case,
then you still have no way of defining the variable. (I also
don't know the status of this proposal, whether it has been
accepted, or is likely to be, or not.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #6
"Stefan Ram" <ra*@zedat.fu-berlin.dewrote in message
ra*@zedat.fu-berlin.de (Stefan Ram) writes:
>>James Kanze <ja*********@gm ail.comwrites:
>>>The declaration of a static variable inside a class
specifier
>>>is not a definition; it requires a definition elsewhere.
But if the type of a variable is »int«, the variable
also can be defined within the class specifier.

Correction: This holds only for static /const/ fields.
Strictly speaking, statement made by James is correct.

Section 9.4.2/4
"If a static data member is of const integral or const enumeration type, its
declaration in the class definition can specify a constant-initializer which
shall be an integral constant expression. In that case, the member can
appear in integral constant expressions within its scope. **The member shall
still be
defined in a namespace scope if it is used in the program and the namespace
scope definition shall not contain an initializer.** "

Here *used* in the program means that the static const member is treated
like an l-value.
--
http://techytalk.googlepages.com
Jun 27 '08 #7
On 13 huhti, 11:16, Rahul <sam_...@yahoo. co.inwrote:
What if i want to use the variable only inside the
function?
Put the class declaration outside the function and the
instance of it inside.
Jun 27 '08 #8
On Apr 13, 5:01 pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
James Kanze <james.ka...@gm ail.comwrites:
The declaration of a static variable inside a class
specifier
is not a definition; it requires a definition elsewhere.
But if the type of a variable is »int«, the variable
also can be defined within the class specifier.
Not according to the standard. Even if an initializer is
present, the declaration in the class is not a definition.
(This is the only case in the language, I think, where an
initializer is present on something that is not a definition.)
According to the standard, it must be defined once and only once
in the program if it is used. According to the original
standard, it is used if its name appears in a potentially
evaluated expression; the latest draft changes this to exclude
cases where the object "satisfies the requirements for appearing
in a constant expression and the lvalue-to-rvalue conversion is
immediately applied."

Not providing a definition (or providing more than one) is
undefined behavior, of course, which means that the
implementation is not required to emit an error message. The
language added to the latest draft corresponds to a case where
(as far as I know) no implementation actually did emit an error
message. If you take the address of the static variable, even
indirectly (e.g. by binding it to a reference), then you will
generally have to provide a definition.

Try the following:

#include <iostream>

struct S
{
static int const i = 47 ;
} ;

void
f( int const& i )
{
std::cout << i << std::endl ;
}

int
main()
{
f( S::i ) ;
return 0 ;
}

It fails to compile (actually, fails to link) with the compilers
available to me (g++, VC++ and Sun CC).

Note too that while typically, one would declare f, above, to
take a value rather than a reference, references can appear
because of templates, e.g. std::vector<int >::push_back( ) takes a
reference to int. In such cases, it's likely that whether the
compiler complains depends on the optimization level, or more
precisely, whether it actually inlines push_back or not).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #9

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

Similar topics

0
8600
by: Steffen | last post by:
Hi! I'm trying to access a EntityBean from a servlet via the bean's local home interface. The EJB and the Servlet are together in one .ear file and I'm using JBoss 3.2.3. I think the "<local-jndi-name>" in jboss-web.xml is wrong. But I don't know what to write there. I've searched for examples or other help, but I haven't found anything useful. Thanks for your help in advance!
3
3656
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore my settings if my profile became tampered with or corrupt. Is there any sample code available out there? -Robert
0
2501
by: Manfred Braun | last post by:
Hi All, I've already asked another question , but this is finally the same issue. I cannot execute two asynchron WMI queries on the local machine, one after the other. The original question is based on complicated code. So I reduced the "kernel" to a small C# sample , which shows the problem. The question is: - Is this a problem with my code, as not freeing resources etc? - Is this possibly a WMI issue?
23
4020
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people who will find them very helpfull. gcc has them as a non-standard option, but only when compiling C language code, so I'm afraid there might be some obscure reason why local functions are not so easy to be dealt with in C++, which I do not yet know.
0
1195
by: shapper | last post by:
Hello, I have a page class and inside it I have a ITemplate class to create the item template of a repeater: Partial Class Contacts Inherits System.Web.UI.Page Private Class myTemplate Implements ITemplate
55
6249
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
1
3638
by: jcprince | last post by:
Hi Not sure I can do what I'm trying to do without using a 3rd party component like Dart. I need to build a windows service to create a socket connection on an IBM mainframe using an IP and port combination. No problem there. However, due to the expected volume (at least 20x10K streams per second in each direction), the mainframe sysadmin has requested the service use multiple 'conversations' within the single IP connection. The...
5
2861
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a app that uses several membership/role providers. I can list these Providers with the code: Dim rootWebConfig1 As Configuration rootWebConfig1 = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath) Dim section As New MembershipSection section = rootWebConfig1.GetSection("system.web/membership")
28
2571
by: cpluslearn | last post by:
Hi, I have a local class inside a function template. I am able to wrap any type in my local class. Is it legal C++? What type of class is Local? Is it a class template or regular class? Thanks in advance. --dhina --------------------------------------------------------------------------------------------------------------------------------------------- class Foo { public:
0
9647
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
9496
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
10164
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
10110
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
9961
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
6745
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
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.