Is this a const class member function

Discussion in 'C++' started by John, Apr 14, 2010.

  1. John

    John Guest

    Hi,

    Suppose I have the following class

    class A
    {
    int a;

    void func1(int& x) const { x = 1; }

    void func2()
    {
    func1(a);
    }

    };

    Should "func1" be a const member function? It doesn't change any
    member data directly, but if called with member data as the argument
    then the class data has changed before/after the call.

    Thanks,
    John
     
    John, Apr 14, 2010
    #1
    1. Advertising

  2. John

    John Guest

    Leigh Johnston wrote:
    >
    >
    > "John" <> wrote in message
    > news:hq4n91$au7$...
    >> Hi,
    >>
    >> Suppose I have the following class
    >>
    >> class A
    >> {
    >> int a;
    >>
    >> void func1(int& x) const { x = 1; }
    >>
    >> void func2()
    >> {
    >> func1(a);
    >> }
    >>
    >> };
    >>
    >> Should "func1" be a const member function? It doesn't change any
    >> member data directly, but if called with member data as the argument
    >> then the class data has changed before/after the call.
    >>
    >> Thanks,
    >> John

    >
    > I would say that it should be const yes but you could also argue that it
    > should be static instead if it doesn't involve any state of A. func2()
    > would not be callable via a const reference to A.
    >
    > /Leigh


    Hi Leigh,

    Are there any disadvanatages to making making a function like this
    static. In other words, do you recommend making all member functions
    static that do not change the state of a class?

    Why would we want func2 to be callable by a const reference to A? Since
    func2 is not 'const', shouldn't we want a const& reference to A to not
    be able to call it?

    Thanks,
    John
     
    John, Apr 14, 2010
    #2
    1. Advertising

  3. John

    John Guest

    >> Are there any disadvanatages to making making a function like this
    >> static. In other words, do you recommend making all member functions
    >> static that do not change the state of a class?

    >
    > No disadvantages as such just make sure the role of your class is
    > clearly defined and it makes sense for the static member functions to be
    > a part of the class rather than being free functions.
    >
    >>
    >> Why would we want func2 to be callable by a const reference to A?
    >> Since func2 is not 'const', shouldn't we want a const& reference to A
    >> to not be able to call it?
    >>

    >
    > Correct, as you couldn't call func2() via a const reference you don't
    > have to worry about the fact that func1() is const.
    >
    > /Leigh


    Thanks,
    John
     
    John, Apr 14, 2010
    #3
  4. John

    John H. Guest

    On Apr 14, 1:42 pm, John <> wrote:
    > do you recommend making all member functions
    > static that do not change the state of a class?


    First you use the word "class" when I think you may have ment
    "object". Think of class as "the cookie cutter" and object as "the
    cookie".
    Second, just because a member function does not change the state of an
    *object* does not mean it can be static.
    Here is a general rule of thumb you could try:
    - Member functions that do not use (read or write) the state of the
    object can be static. (There is no "the object")
    - Member functions that want to know, but not change, the state of the
    object can be const.
    - Member functions that need to change the state of the object should
    be non-static non-const.

    Example pseudocode:

    class Color
    {
    private:
    int red;
    int green;
    int blue;
    public:
    Color(int red, int green, int blue) :
    red(red),
    green(green),
    blue(blue)
    {
    }

    // neither const nor static because it modifies member variables
    void set_color(int red, int green, int blue)
    {
    this->red = red;
    this->green = green;
    this->blue = blue;
    }

    // const because it only reads from member variables
    void get_color(int & red, int & green, int & blue) const
    {
    red = this->red;
    green = this->green;
    blue = this->blue;
    }

    // static because it doesn't use any non-static functions or
    variables
    // i.e. it is related to the Color concept, but not to any
    existing Color object
    static Color get_black()
    {
    return Color(0,0,0);
    }
    };

    Now, there are things called "static member variables". These are not
    part of any particular object, although you might think of them as
    part of "the state of the *class*". These can be read/written by
    static member functions, const member functions, and non-const/non-
    static member functions. Also (depending on the access) these can be
    read/written by non-member functions and member functions from other
    classes.

    Also one more clarification for static member functions: when I saw
    that they "can't use non-static member functions or variables", by
    this I mean that they cannot use such things directly (because there
    is no "this" object). They can however use such things indirectly,
    i.e. through an object of the class. For instance our Color class
    might have another static member function called get_white:

    static Color get_white()
    {
    // For demonstration purposes here, assume Color
    // has only a default constructor:
    Color c;
    c.set_color(255, 255, 255); // ok to call set_color because it is
    indirect
    return c;
    }

    The same thing with const member functions. They can modify the state
    of other objects, just not there own. e.g.:

    bool Color::isBlackOrWhite() const
    {
    // For this assume I have an equality operator defined
    Color compare_color = make_black();
    if(*this == compare_color)
    {
    return true;
    }
    compare_color.set_color(255, 255, 255); // ok to call non-const
    function because it is not us
    return *this == compare_color;
    }
     
    John H., Apr 14, 2010
    #4
    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. Polanski24
    Replies:
    1
    Views:
    571
    Rolf Magnus
    Apr 11, 2005
  2. David Scarlett
    Replies:
    3
    Views:
    864
    mlimber
    Feb 7, 2006
  3. Javier
    Replies:
    2
    Views:
    627
    James Kanze
    Sep 4, 2007
  4. fungus
    Replies:
    13
    Views:
    954
    fungus
    Oct 31, 2008
  5. Markus Keppeler
    Replies:
    13
    Views:
    959
    Stuart Redmann
    Jul 29, 2011
Loading...

Share This Page