473,398 Members | 2,389 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,398 software developers and data experts.

Can static member functions be const?

Hi,all.
The section 13.5.1 of the <C++ primer 3rd editionsays, a static
member frunction may not be declared as const or volatile. I could not
explain to myself why? The constness seems to be irrelevant to whether
the function is static or non-static.
Furthermore, i am not sure the following program is correct. According
to the book, it should be wrong, but my compiler says it is right.

class Account
{
public:
static const double interest() {return _interestRate;}
private:
static double _interestRate; //I know i should declared it as
const, but it is also OK.
};

double Account::_interestRate = 1;

int main()
{
Account::interest();
return 0;
}

Apr 19 '07 #1
2 2300
mimi wrote:
Hi,all.
The section 13.5.1 of the <C++ primer 3rd editionsays, a static
member frunction may not be declared as const or volatile. I could not
explain to myself why? The constness seems to be irrelevant to whether
the function is static or non-static.
The constness of a member function relates to the instance of the class
it is called on ('this'). A const member function can not change the
state of the class instance. A static member function is not called on
an instance of a class, so it can't be const.
Furthermore, i am not sure the following program is correct. According
to the book, it should be wrong, but my compiler says it is right.

class Account
{
public:
static const double interest() {return _interestRate;}
The *function* isn't const here, the *return* type is.

--
Ian Collins.
Apr 19 '07 #2
On Apr 19, 10:56 am, Ian Collins <ian-n...@hotmail.comwrote:
mimi wrote:
Hi,all.
The section 13.5.1 of the <C++ primer 3rd editionsays, a static
member frunction may not be declared as const or volatile. I could not
explain to myself why? The constness seems to be irrelevant to whether
the function is static or non-static.

The constness of a member function relates to the instance of the class
it is called on ('this'). A const member function can not change the
state of the class instance. A static member function is not called on
an instance of a class, so it can't be const.
Wonderful reply. Thank you.
>
Furthermore, i am not sure the following program is correct. According
to the book, it should be wrong, but my compiler says it is right.
class Account
{
public:
static const double interest() {return _interestRate;}

The *function* isn't const here, the *return* type is.
Oh,yes. To make the function const, i should declared it as static
interest() const;
>
--
Ian Collins.

Apr 19 '07 #3

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

Similar topics

2
by: Rahul Joshi | last post by:
Hi, Is it possible to define static member functions that are 'const', i.e. they just read but do not modify the static data members of a class? Declaring functions like: class SomeClass {...
5
by: Naren | last post by:
Hello Grp, Correct me if I am wrong. static member functions can act only on static member varaibles.It can accessed by using the name of the class. Then why is there an access controller. ...
3
by: exits funnel | last post by:
Hello, One of the problems at the end of Chapter 14 in Bruce Eckel's thinking in C++ reads as follows: Create a class with two static member functions. Inherit from this class and redefine...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
3
by: qWake | last post by:
The C++ language standard stipulates at section 9.4.1 that " A static member function shall not be declared const " The question is: what problem(s) could possibly exist in allowing static...
1
by: RainerFaulstich | last post by:
Hi, Some dummy question : Are static member functions of a class indeed a single instance for all instances of the class or have difference instances of this class have its own instances of this...
3
by: paul.furber | last post by:
Hi all, I have some code which looks a bit like this: #define Offset(m, T) ((size_t)(&((T *)1)->m) - 1) class Point: private: int *x,*y;
9
by: tkrogc | last post by:
I compiled the source below using the comeau compiler http://www.comeaucomputing.com/tryitout/ Why does the f funtion compile while the g function produces an lvalue error ? (I thought the two...
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
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...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.