static const function not allowed?

S

Simon

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
 
?

=?ISO-8859-15?Q?Stefan_N=E4we?=

Simon said:
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 ';'
> 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
 
J

Jonathan Mcdougall

Simon said:
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
 
I

Ian Collins

Simon said:
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...
 
S

Simon

Ian Collins said:
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
 
I

Ian Collins

Simon said:
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.
 
J

Jonathan Mcdougall

Ian said:
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
 
E

Earl Purple

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.
 
R

Rolf Magnus

Earl said:
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.
 
E

Earl Purple

Rolf said:
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".
 
M

mlimber

Earl said:
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".

See also this message:

http://groups.google.com/group/comp.lang.c++.moderated/msg/d09e324fd0179a3f

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top