473,672 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static const function not allowed?

Hi,

I have something like

//
// common.h
const unsigned long m_dwStyle = 0x123;

//
// common.h
static BOOL Style( DWORD dw )const
{
return ((m_dwStyle&dw) ==dw);
};
//
//

But my compiler tells me "modifiers not allowed on static member functions".
But I cannot see what is wrong with the above.

Simon
May 10 '06 #1
10 21334
Simon wrote:
Hi,

I have something like

// common.h
static BOOL Style( DWORD dw )const
{
return ((m_dwStyle&dw) ==dw);
};

But my compiler tells me "modifiers not allowed on static member functions".
But I cannot see what is wrong with the above.


I suspect the function is defined inside a class or else the error
would probably be different. The "const" modified is only allowed on
non-static member functions. Think: a const member function is not
allowed to modify the object it is called on, but static member
functions are not called on an object. Having a const static member
function makes no sense, hence it is illegal.
Jonathan

May 10 '06 #2
Simon wrote:
Hi,

I have something like

//
// common.h
const unsigned long m_dwStyle = 0x123;

//
// common.h
static BOOL Style( DWORD dw )const
{
return ((m_dwStyle&dw) ==dw);
};
//
//

But my compiler tells me "modifiers not allowed on static member functions".
But I cannot see what is wrong with the above.

A static member is a member of the class, not an instance of a class, so
it can't be const as there isn't an object to be const with...
--
Ian Collins.
May 10 '06 #3
"Ian Collins" <ia******@hotma il.com> wrote in message
news:4c******** *****@individua l.net...

But my compiler tells me "modifiers not allowed on static member
functions".
But I cannot see what is wrong with the above.

A static member is a member of the class, not an instance of a class, so
it can't be const as there isn't an object to be const with...


I see,

so what would be the 'preferred definition?

BOOL Style( DWORD dw )const;
// or
static BOOL Style( DWORD dw );

Regards,

Simon
May 10 '06 #4
Simon schrieb:
Hi,

I have something like
Tell us exactly what you have (do copy & paste)

//
// common.h
const unsigned long m_dwStyle = 0x123;

//
// common.h
static BOOL Style( DWORD dw )const
{
return ((m_dwStyle&dw) ==dw);
}; <nitpick>
Superfluous ';'
</nitpick>
But my compiler tells me "modifiers not allowed on static member functions".
But I cannot see what is wrong with the above.


The compiler tells you exactly:
"Static member functions can not be const."

If a member function is const it tells the compiler that it does not modify
any data members of the class. But since a static member function can not
access any data member of that class there's no sense in making it
const.

HTH
Stefan
--
Stefan Naewe
naewe.s_AT_atla s_DOT_de
May 10 '06 #5
Simon wrote:
"Ian Collins" <ia******@hotma il.com> wrote in message
news:4c******** *****@individua l.net...
But my compiler tells me "modifiers not allowed on static member
functions" .
But I cannot see what is wrong with the above.


A static member is a member of the class, not an instance of a class, so
it can't be const as there isn't an object to be const with...

I see,

so what would be the 'preferred definition?

BOOL Style( DWORD dw )const;
// or
static BOOL Style( DWORD dw );

Well it the function uses any non-static class data it must be a member,
otherwise it may as well be static.

--
Ian Collins.
May 10 '06 #6
Ian Collins wrote:
Simon wrote:
Hi,

I have something like

//
// common.h
const unsigned long m_dwStyle = 0x123;

//
// common.h
static BOOL Style( DWORD dw )const
{
return ((m_dwStyle&dw) ==dw);
};
//
//

But my compiler tells me "modifiers not allowed on static member functions".
But I cannot see what is wrong with the above.
A static member is a member of the class, not an instance of a class,


I think this should be "A static member is a member of the class, not a
member of an instance".

Though I understand what you mean, this is kind of wrong. A static
member function in C++ is still a member function and there is no such
thing as an "instance member function".
so it can't be const as there isn't an object to be const with...

Jonathan

May 10 '06 #7
Actually it might make sense to mean "a static const member function
cannot modify any other static members".

But they haven't defined it that way.

May 10 '06 #8
Earl Purple wrote:
Actually it might make sense to mean "a static const member function
cannot modify any other static members".

But they haven't defined it that way.


It wouldn't be too useful though. For non-static members, the important
aspect is that it can be used on const objects (which of course means that
it doesn't modify the object). Since there is no object in a static member
functions, there is no real use for const.

May 10 '06 #9

Rolf Magnus wrote:
Earl Purple wrote:
Actually it might make sense to mean "a static const member function
cannot modify any other static members".

But they haven't defined it that way.


It wouldn't be too useful though. For non-static members, the important
aspect is that it can be used on const objects (which of course means that
it doesn't modify the object). Since there is no object in a static member
functions, there is no real use for const.


It might have a use in meta-programming where you might use different
overloads, particularly in a multi-threaded environment. (A "const"
method would require only a read-lock whereas a "non-const" method
would require a write lock).

Of course, if that's what you want your design is probably wrong, and
I'm not intending to propose that they bring it into the standard. Am
just saying that it is not necessarily true that it "doesn't make
sense".

May 10 '06 #10

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

Similar topics

30
3471
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
8
1826
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances' handles) instances? Thanks in advance for any help
7
2827
by: Michael Klatt | last post by:
Is there any practical difference between a local variable in main() declared 'const' and one declared 'static const'? int main() { static const int i1(0); const int i2(0); return 0; }
5
1705
by: Minti | last post by:
How do we initialize static const data in C++, I tried class Foo { static const std::string name = "MyName"; }; I don't see any reason as to why this must not work.
4
5422
by: cppsks | last post by:
"Defining static const variables inside the class is not universally supported yet, so for now I guess you'll have to move the definition out of the body of the class. No, static const inside classes is only allowed for integral consts, like static const enum and static const int, not for arrays and structs. It does make sense for an array of integral consts though." I read the above statements in this group a while back. First off,...
14
2860
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
4
6189
by: Rakesh Sinha | last post by:
I am having this code here. static const float PI = 3.14159; static const float INC = 0.4f * PI; When I compile my program, I get the following error, error: `MyClass::PI' cannot appear in a constant-expression .
2
3582
by: Drew McCormack | last post by:
I am getting an error in g++ 4.0.0 that I did not get in g++ 3.4. I have a header with the following const variables with namespace scope: namespace Periphery { extern const double ProtonMassInAtomicUnits = 1836.152755656068; } I try to use these in another header to initialize static const member
13
6580
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm trying to use from an executable, compile using gcc 4.1.2. Everything works fine until I try specializing one of the static member function templates in a non-template class. I have a feeling I'm messing up something obvious so before I post a...
0
8486
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
8406
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,...
1
8609
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
8683
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...
1
6240
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5707
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
4230
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...
0
4419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2821
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

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.