Quick C++ question

Discussion in 'OT Technology' started by mrburner, Sep 15, 2003.

  1. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    i have a program that has a logbook class and has these function prototypes...i need to know what the significance of the keyword const is in these prototypes and why some are member functions const and others are not

    int getMonth () const; //return the month
    int getYear () const; //return the year
    int getDaysInMonth () const; //number of days in month

    any help is appreciated...thanks
     
  2. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Wow its been a while since I've played with C++.

    The "const" means its a constant. As you probably know, a variable that is declared a constant can not be changed. C++ lets you declare classes and methods as constants. By declaring the method as a const, you're telling the compiler the method's code should not modify any of the objects variables. And if a const method tries to, the compiler will catch the error (otherwise, you might have a bug on your hands). A good example of using this would be anytime you have a method that just returns a value (such as a member variable), but shouldn't be allowed to make any changes to any of the variables belonging to the object. That make sense?
     
  3. crotchfruit

    crotchfruit Guest

    this was an interview question i had once :eek3:
     
  4. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    :cool: I think it was on my AP exam.
     
  5. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,924
    Likes Received:
    11
    Location:
    Los Angeles
    Declare a constant when the function will not be modified in anyway. IE: The data within the function will not be changed by the user or any other functions.
     
  6. tantrum

    tantrum Guest

    these functions will not modify the object. common in accessor functions such as those you listed, they just return the value of the data member and do not modify it.
     
  7. GmH

    GmH Guest

    Essentially, it is an indicator that the member function does not alter the state of the object. This means the function can be called on a const object, cannot alter any data members (unless they are declared as mutable), and cannot call any non-const member functions.

    e.g.

    class A
    {
    private:
    int i;
    mutable int j;
    public:
    void foo() {
    i = 1; // ok
    }
    void bar() const {
    foo(); // error, can't call non-const method from const method
    i = 2; // error, can't modify data members in const method
    j = 3; // ok, because j is declared mutable
    }
    };

    void f()
    {
    const A a;
    a.bar(); // ok
    a.foo(); // error, can't call non-const method of const object
    }
     

Share This Page