473,396 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function prototype & implementation mismatch

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
Jul 22 '05 #1
2 2624
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?


Jul 22 '05 #2
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.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
6
by: dndfan | last post by:
Hello, In the short time I have spent reading this newsgroup, I have seen this sort of declaration a few times: > int > func (string, number, structure) > char* string > int number >...
11
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
8
by: vaib | last post by:
hi all , It really seems that C never ceases to amaze . All this time i've been doing C and i thought i was quite adept at it but i was wrong . So without wasting any more time , here's the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.