473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2242
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.comwrote:
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 objektorientierter Datenverarbeitung
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*********@gmail.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...@gmail.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 objektorientierter Datenverarbeitung
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
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...
3
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...
0
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...
23
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...
0
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...
55
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...
1
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...
5
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 =...
28
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.