Thread: confused: static method const???

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    Unhappy confused: static method const???

    I'm working on this program and is such a slow progress, I keep stumbling on so small things... Like this one. My textbook is showing a class with private static int, and a public static method which is also const as it just displays the private member... I declared my own class, following the guidelines from the textbook and when I compile it (DevC++ 4.9.9.2) it tells me 34 C:\Dev-Cpp\inheritance_project\main.cpp static member function `static int MyString::numStrings()' cannot have `const' method qualifier
    Why is that?
    I wrote it as such:
    Code:
    private:
    static int counter;
    public:
    static int numStrings(void)  const { return counter; }
    What's wrong then???

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    const in that context implies that the object calling it will not be modified, but since static functions are not called with an object, there's no logical reason to declare as such.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    OK... that sounds reasonable. Does most of compliers give errors at this point? Or is it only Dev?

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Dev-CPP is not a compiler. It's merely an IDE, using the MinGW port, of the GNU GCC compiler. GCC is pretty standards compliant and widely used, meaning: you might as well try fix your code, not the compiler.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by kocika73
    OK... that sounds reasonable. Does most of compliers give errors at this point? Or is it only Dev?
    as jaket said, devcpp is an IDE not a compiler.

    C++ compilers are required to complain (or, at least, fail to compile) when they encounter a static const function.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    So the book is wrong then.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Out of curiosity, what book might that be?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by kocika73
    So the book is wrong then.
    If "the book" claims a static const member function is valid C++ then, yes, it is wrong. If a code example in the book is of that form, then that example is wrong (which may mean the book is crap, or may just be an error in the particular example).

    Like laserlight, I'm curious about what book you're using.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    It is Object-Oriented Programming in C++ by Johnsonbaugh and Kalin. Example 3.7.4 on p. 154 (the top of page. Later they modify that without explaining why they leave out the "const")

  10. #10
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    If they later drop the const all points out that it is a typo. It's probably already corrected in later editions (if there are any) or on some online errata, otherwise you might wanna consider sending the author an e-mail with that info.

    Cheers

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Non-static member functions get passed a pointer to the object they are operating on, called this. All the function's access to its class is through this pointer. A function declared like this:
    Code:
    void func() const {}
    has its this pointer declared as
    Code:
    classname *const this
    which prevents the function from changing the class's data.

    Since static functions aren't passed a this pointer (that's what makes them static), you cannot declare a static function to be const.
    Code:
    static void constfunc() const {}  /* error */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM