Can static member functions be const?

Discussion in 'C++' started by mimi, Apr 19, 2007.

  1. mimi

    mimi Guest

    Hi,all.
    The section 13.5.1 of the <C++ primer 3rd edition> says, 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;
    }
     
    mimi, Apr 19, 2007
    #1
    1. Advertising

  2. mimi

    Ian Collins Guest

    mimi wrote:
    > Hi,all.
    > The section 13.5.1 of the <C++ primer 3rd edition> says, 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.
     
    Ian Collins, Apr 19, 2007
    #2
    1. Advertising

  3. mimi

    mimi Guest

    On Apr 19, 10:56 am, Ian Collins <> wrote:
    > mimi wrote:
    > > Hi,all.
    > > The section 13.5.1 of the <C++ primer 3rd edition> says, 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.
     
    mimi, Apr 19, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cheeser
    Replies:
    17
    Views:
    771
    cpp_weenie
    Oct 18, 2003
  2. puzzlecracker
    Replies:
    2
    Views:
    415
    Siemel Naran
    Feb 7, 2005
  3. Javier
    Replies:
    2
    Views:
    622
    James Kanze
    Sep 4, 2007
  4. dolphin
    Replies:
    3
    Views:
    1,401
    Pete Becker
    Dec 5, 2007
  5. fungus
    Replies:
    13
    Views:
    948
    fungus
    Oct 31, 2008
Loading...

Share This Page