const in function header

This is a discussion on const in function header within the C++ Programming forums, part of the General Programming Boards category; What are different possibilities that a const can appear in a function header, and what does each one do? Also, ...

  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,344
    >> 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
    Portugal
    Posts
    7,579
    > 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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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, 07:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  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, 08:05 AM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM

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