Thread: const in function header

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    const in function header

    What are different possibilities that a const can appear in a function header, and what does each one do? Also, is something like this is possible?
    Code:
    void foo(int *a, int *b)*c
    {
       ...
    }
    If so then what does that *c do there?

    Moreover, in the following case (which I know can happen):
    Code:
    struct my_struct
    {
       ...
    }*z;
    What's the function of *z?

    Any help is appreciated.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> What are different possibilities that a const can appear in a function header,
    It can make a parameter const, it can make a return value const, and if the function is a member function it can make the function const within a class/struct.

    >> is something like this is possible?
    No.

    >> What's the function of *z?
    It is the same as saying my_struct* z; which is to declare a pointer to my_struct variable named z.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > What are different possibilities that a const can appear in a function header, and what does each one do?

    When the function is a member of a class or struct and it will not alter the state of said class or struct.

    EDIT: Daved reply to this question is more complete, than mine

    > If so then what does that *c do there?

    it generates a compile-time error.

    > What's the function of *z?

    to also generate a compile time error
    Kidding... creates a pointer to my_struct called z.
    Last edited by Mario F.; 11-10-2006 at 11:59 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM