Function prototype & implementation mismatch

Discussion in 'C++' started by News, May 17, 2004.

  1. News

    News Guest

    Hi,

    Should the following code compile without errors?

    class test
    {
    public:
    void display(const int x);
    };

    void test::display(int num)
    {
    }

    void main()
    {
    test object;
    object.display(10);
    }

    The function prototype in the class declaration differs from the
    implementation as the argument is const in the declaration but not in the
    implementation (or the other way round). I know that this is slightly
    superfluous as the int is being passed by value anyway & so cannot be
    altered in the display() function as a return value. However, there is
    still a mismatch! If the argument to display is passed by reference rather
    than by value i.e.

    class test
    {
    public:
    void display(const int & x);
    };

    void test::display(int & num)
    {
    }

    void main()
    {
    test object;
    object.display(10);
    }

    then a compiler error is generated.

    I am using the Microsoft C++ compiler (VC++6 & VC++.NET). What do other
    compilers do with the first code sample?

    Simon
     
    News, May 17, 2004
    #1
    1. Advertising

  2. News wrote:
    > Hi,
    >
    > Should the following code compile without errors?
    >
    > class test
    > {
    > public:
    > void display(const int x);
    > };
    >
    > void test::display(int num)
    > {
    > }
    >
    > void main()


    main *must* return int
    > {
    > test object;
    > object.display(10);
    > }


    As a reference point, this compiles without error in gcc 3.3.1 and gcc
    3.4.0.

    >
    > The function prototype in the class declaration differs from the
    > implementation as the argument is const in the declaration but not in the
    > implementation (or the other way round). I know that this is slightly
    > superfluous as the int is being passed by value anyway & so cannot be
    > altered in the display() function as a return value. However, there is
    > still a mismatch! If the argument to display is passed by reference rather
    > than by value i.e.
    >
    > class test
    > {
    > public:
    > void display(const int & x);
    > };
    >
    > void test::display(int & num)
    > {
    > }
    >
    > void main()
    > {
    > test object;
    > object.display(10);
    > }
    >
    > then a compiler error is generated.


    gcc 3.4.0 also generates a compile error.


    >
    > I am using the Microsoft C++ compiler (VC++6 & VC++.NET). What do other
    > compilers do with the first code sample?
    >
     
    Gianni Mariani, May 17, 2004
    #2
    1. Advertising

  3. News

    Fraser Ross Guest

    It makes no difference to the caller whether a value parameter is const or
    not. This is why it compiles and why I take the advice to make declarations
    of value parameters non const.

    Fraser.
     
    Fraser Ross, May 17, 2004
    #3
    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. ian douglas
    Replies:
    0
    Views:
    1,847
    ian douglas
    Aug 19, 2003
  2. June Lee
    Replies:
    2
    Views:
    847
    Jim Cobban
    Apr 13, 2008
  3. Aaron DeLoach
    Replies:
    2
    Views:
    228
    Ben Morrow
    Jun 5, 2004
  4. Brian Wakem

    Prototype mismatch under ModPerl::PerlRun

    Brian Wakem, Sep 6, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    448
    Brian Wakem
    Sep 6, 2005
  5. Replies:
    9
    Views:
    234
    Thomas 'PointedEars' Lahn
    May 26, 2006
Loading...

Share This Page