access modifier after function name in declaration

This is a discussion on access modifier after function name in declaration within the C++ Programming forums, part of the General Programming Boards category; Code: class Person: public Sortable { int getsignature() const; //what is the significance the const here? } Will someone please ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    access modifier after function name in declaration

    Code:
    class Person: public Sortable
    {
       int getsignature() const;  //what is the significance the const here?
    }
    Will someone please tell me what the 'const' after the 'getsignature()' in the above code does?
    Thanks.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    It means that the function is not going to modify the particular instance of the object calling the function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    The const applies to the calling objects. If you have a member function that should not change the value of the calling object then you put the const. Then you will receive a error if your function changed the value of the calling object.
    Last edited by stumon; 08-05-2003 at 10:01 AM.
    The keyboard is the standard device used to cause computer errors!

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Re: access modifier after function name in declaration

    Originally posted by kes103
    Code:
    class Person: public Sortable
    {
       int getsignature() const;  //what is the significance the const here?
    }
    Will someone please tell me what the 'const' after the 'getsignature()' in the above code does?
    Thanks.
    As stated, it means that getsignature() will not modify the variables within the calling Person object (it is allowed to modify volatile variables).

    The keyword means the method itself is const, so it cannot modify the object it operates on. Methods can be broken into two categories: modifiers, which change the state of the object, and accessors, which merely allow an outsider to view parts of the state of the object. Modifiers cannot be const, accessors should be.

    A const object can only be used to call const functions. For example:

    Code:
    void F(const Person & p){
      p.getsignature(); // this is legal because getsignature is const
    }

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Also, I'm sure this will trip you up at some point:

    Only member functions of a class can be labeled const--not other functions.
    Last edited by 7stud; 08-05-2003 at 12:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21