const function?

Discussion in 'C++' started by franco ziade, Feb 25, 2005.

  1. franco ziade

    franco ziade Guest

    what does this declaration mean in C++

    void func_name() const
    { ... }

    is this a const function ?
    what are the implications?

    thanks
    franco ziade, Feb 25, 2005
    #1
    1. Advertising

  2. franco ziade

    Tim Love Guest

    Tim Love, Feb 25, 2005
    #2
    1. Advertising

  3. "franco ziade" <> skrev i en meddelelse
    news:6qGTd.48193$...
    > what does this declaration mean in C++
    >
    > void func_name() const
    > { ... }
    >
    > is this a const function ?
    > what are the implications?
    >
    > thanks
    >
    >

    This does not compile. Probably from a textbook where "..." intends to mean
    that the body is left out.

    /Peter
    Peter Koch Larsen, Feb 25, 2005
    #3
  4. franco ziade wrote:
    > what does this declaration mean in C++
    >
    > void func_name() const
    > { ... }
    >
    > is this a const function ?


    It means that func_name() does not change *this;

    You can think of the "this" pointer being an invisible paramter to the
    function.

    void func_name( T * this ) (no const)
    void func_name( const T * this ) (const)

    > what are the implications?


    You can use this to discriminate between a const function call and a
    non-const function call.

    i.e.
    struct A { void f () const; void f(); };

    int main()
    {
    A a;
    const A& ca = a;

    a.f(); // calls void f()
    ac.f();// calls void f () const;
    }


    >
    > thanks
    >
    >
    Gianni Mariani, Feb 25, 2005
    #4
  5. franco ziade wrote:
    > what does this declaration mean in C++
    >
    > void func_name() const
    > { ... }
    >


    The const only really makes sense in member functions of classes. It
    means that this class won't alter the state of the class. You should try
    to mark all functions const which are const :)

    For example, consider:

    class S {
    int i;
    public:
    int get() const { return i; }
    void put(int j) { i=j; }
    };

    notice that I made get() const as it doesn't effect the state of the
    class. Put is not const (and if you tried making it const it wouldn't
    compile)

    Now consider this function

    void print(const S& s)
    { std::cout << s.get(); }

    If get() wasn't const, this wouldn't compile, you you would be calling a
    non-const member function on a const object.

    One particular point of note is that you should be sure to make ==, !=,
    <, and the other relational operators on classes const (assuming of
    course they are), as many of the implementations of the standard
    algorithms assume (and I believe they are allowed to assume) they can
    put a const on things without effecting anything.

    One final point. You can do:

    public S
    {
    int i;
    public:
    int get() const {return i;}
    int get() {i=0; return i;}
    };

    ie you can define the const and non-const version of a function
    differently, and give them different meanings. You DON'T have to do this
    just so non-const objects have a function (the const version can still
    be used on non-const Ss).

    Now different things will happen depending on is some instance of S is
    const or not. Of course I've only ever seen someone write this once when
    they weren't just messing about, and I was forced to kill them and bury
    them under the patio so no other such code could ever escape. Don't make
    me do the same thing to you!

    Chris
    Chris Jefferson, Feb 25, 2005
    #5
  6. Chris Jefferson wrote:
    .... Of course I've only ever seen someone write this once when
    > they weren't just messing about, and I was forced to kill them and bury
    > them under the patio so no other such code could ever escape. Don't make
    > me do the same thing to you!


    I'm running... :)

    How about this:

    class Buffer
    {
    char * buf;

    public:

    Buffer( char * buf )
    : buf( buf )
    {
    }

    char * Get() { return buf; }
    const char * Get() const { return buf; }
    };

    Sometimes (this is a simple case) you do want to do somthing different.

    const containers usually want to do somthing very different with accessors.
    Gianni Mariani, Feb 25, 2005
    #6
  7. franco ziade

    Shezan Baig Guest

    Peter Koch Larsen wrote:
    > "franco ziade" <> skrev i en meddelelse
    > news:6qGTd.48193$...
    > > what does this declaration mean in C++
    > >
    > > void func_name() const
    > > { ... }
    > >
    > > is this a const function ?
    > > what are the implications?
    > >
    > > thanks
    > >
    > >

    > This does not compile. Probably from a textbook where "..." intends

    to mean
    > that the body is left out.
    >
    > /Peter




    Thanks for pointing that out, Peter. :)
    Shezan Baig, Feb 26, 2005
    #7
  8. Gianni Mariani wrote:
    > Chris Jefferson wrote:
    > ... Of course I've only ever seen someone write this once when
    >
    >> they weren't just messing about, and I was forced to kill them and
    >> bury them under the patio so no other such code could ever escape.
    >> Don't make me do the same thing to you!

    >
    >
    > I'm running... :)
    >
    > How about this:
    >
    > class Buffer
    > {
    > char * buf;
    >
    > public:
    >
    > Buffer( char * buf )
    > : buf( buf )
    > {
    > }
    >
    > char * Get() { return buf; }
    > const char * Get() const { return buf; }
    > };
    >
    > Sometimes (this is a simple case) you do want to do somthing different.
    >
    > const containers usually want to do somthing very different with accessors.
    >


    *slaps forehead*.. thats what I get for ever saying "Don't do X in C++"
    for any X :)

    Yes of course, I should have said (thanks!) that you can overload
    functions differently to preserve const correctness, and in fact all the
    standard contaners do that...

    Chris
    Chris Jefferson, Feb 26, 2005
    #8
  9. franco ziade

    Shailendra Kesarwani

    Joined:
    Jan 18, 2011
    Messages:
    1
    Can you please explain which buffer does the constant function return and why?
    Also why does it return a const pointer?
    I think the non-const function would return class variables value.
    Shailendra Kesarwani, Jan 18, 2011
    #9
    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. Replies:
    11
    Views:
    1,101
  2. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,114
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    886
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    540
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page