Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function prototype & implementation mismatch

Reply
Thread Tools

Function prototype & implementation mismatch

 
 
News
Guest
Posts: n/a
 
      05-17-2004
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


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      05-17-2004
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?
>




 
Reply With Quote
 
 
 
 
Fraser Ross
Guest
Posts: n/a
 
      05-17-2004
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class prototype vs C function prototype June Lee C++ 2 04-13-2008 08:17 PM
relation between prototype and Prototype.js shypen42@yahoo.fr Javascript 9 05-26-2006 01:13 AM
Prototype mismatch under ModPerl::PerlRun Brian Wakem Perl Misc 3 09-06-2005 06:05 PM
Prototype mismatch: sub main::head vs ($) at ... Aaron DeLoach Perl Misc 2 06-05-2004 09:47 PM
mod_perl errors: prototype mismatch ... during global destruction ian douglas Perl 0 08-18-2003 11:17 PM



Advertisments