CPB Mailing List

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: const keyword position



const int foo( int i );
// here you are saying that foo returns a const int
int foo (int i) const;
/* but here you are saying that the function is const, I believe this
only makes sense in *object's member functions*.  Thus this function can
not modify any member variables of it's object (unless they are mutable
[or you can use type casting to get around it]), and you can call it on
a const instance of an object. */

class tuition {
public:
    tuition() { t = 2000; }
    void setTuition (int p) { t = p; }
    int getTuition () const { return t; }
private:
    int t; };

void main() {
    tuition sucks;
    sucks.setTuition(0);
    sucks.getTuition();
    const tuition reality;
    reality.getTuition();  // ok getTuition is a const member function
    reality.setTuition(0); // Error, setTuition is not a const mem.func.
                           //and reality is a const instance
}

Hope that helps,

Fernando


W Komornicki's Home Page | Main Index | Thread Index