cppNEWBIE "bool Save(const char *name) const;" why const???

This is a discussion on cppNEWBIE "bool Save(const char *name) const;" why const??? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am new to cpp I have some programs and have a question about " identifiers at the end ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question cppNEWBIE "bool Save(const char *name) const;" why const???

    Hi,
    I am new to cpp
    I have some programs and have a question about "identifiers at the end of the method name"
    When and why are they necessary?
    For example, Consider the following code
    Code:
    class Table1D
    {
    ...
    	bool Save(const char *name) const;	
    ...
    }
    In other languages I will simply write something like
    Code:
     bool Save(const char *name)
    (without the const identifier)
    Another example is here:
    Code:
    float Triangle(const blob &p1, const blob &p2) const { return 0.5f*fabs((p1.x-x)*(p2.y-y)-(p1.y-y)*(p2.x-x)); }
    Why "const" goes to the end, and where should I put an identifier in the end?

    Thanks.
    Mac OS 10.6 Snow Leopard : Darwin

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This means that this is const in the method, i.e the method will not modify the class members. Therefore you can call the Save method even if you don't have the right to modify the Table1D instance:

    Code:
    void foo(const Table1D& table)
    {
        ...
        table.Save(); //OK, won't modify table
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    If we consider that Save lacked a const modifier at the end, the following would not compile:
    Code:
    void foo(const Table1D& table)
    {
        ...
        table.Save(); // Not OK, since we cannot promise Save won't modify table.
        ...
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Wink ok

    thanks anon and Elysia.
    Mac OS 10.6 Snow Leopard : Darwin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The UNIX System Interface
    By rrc55 in forum C Programming
    Replies: 1
    Last Post: 10-20-2009, 05:56 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

Tags for this Thread


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