473,378 Members | 1,209 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,378 software developers and data experts.

const int Foo in paramater list? Question of style

In my function/method paramater list I try to keep constant correctness.
I.E. I would use:
int Foo( const char* a, int b )
instead of
int Foo( char* a, int b )

My question reguards int b. Would it be more correct to make it
int Foo( const char* a, const int b )
instead? I do know the effect of this would be that I can't change b in the
function, which I normally never do anyway.

Can anyone think of a compelling reason to use one over the other? Or is it
just a matter of style?

Thanks.
Sep 20 '07 #1
2 1870
On Sep 21, 5:15 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
In my function/method paramater list I try to keep constant correctness.
I.E. I would use:
int Foo( const char* a, int b )
instead of
int Foo( char* a, int b )

My question reguards int b. Would it be more correct to make it
int Foo( const char* a, const int b )
instead? I do know the effect of this would be that I can't change b in the
function, which I normally never do anyway.

Can anyone think of a compelling reason to use one over the other? Or is it
just a matter of style?
As far as the compiler is concerned, the following have the same
function signature:

void foo(int a);
void foo(const int a);

The compiler essentially ignores the "const" (except inside the
function body, as you already noted). This can be surprising to some
people, so my vote is to not use const on non-pointer/non-reference
types. If you want to enforce const within your function body, you can
use a const reference to the parameter and any decent compiler should
optimize so that there is no difference.

--
Computational Modeling, CSIRO (CMIS)
Melbourne, Australia

Sep 20 '07 #2
On Sep 20, 9:15 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
In my function/method paramater list I try to keep constant correctness.
I.E. I would use:
int Foo( const char* a, int b )
instead of
int Foo( char* a, int b )
My question reguards int b. Would it be more correct to make it
int Foo( const char* a, const int b )
instead? I do know the effect of this would be that I can't change b in the
function, which I normally never do anyway.
Can anyone think of a compelling reason to use one over the
other? Or is it just a matter of style?
It's largely a matter of style. As Craig Scott pointed out, top
level const is ignored in the declaration. And since the
argument is passed by value, you have a local copy; whether you
modify it or not is irrelevant to the client, since he won't see
any of the modifications anyway.

I think most programmers just ignore const completely here. For
those who want the const in the definition (where it is
significant, and can provide information to whoever reads the
function body), there are two schools of thought:
-- only use it in the definition, since it is irrelevant (and
an implementation detail, which may change) to the user, or
-- use it in the declarations as well, so that the declarations
and the definitions are identical.
(I rather prefer the first, but in practice, like most people I
know, I don't use const here.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 21 '07 #3

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

Similar topics

12
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ so maybe there's something I miss. I write a simple board game. It has a board class. This class has a method that returns the count of pieces a player has on the...
4
by: cpisz | last post by:
At least that is what I think I want to do. What is the proper way for me to return multiple data member objects from an accessor method in my class while ensuring the data does not get changed,...
1
by: electric sheep | last post by:
Hi, can somebody explain the following syntax to me. This is straight from a gnu info file: int main(void) { /* Hashed form of "GNU libc manual". */ const char *const pass =...
2
by: Max Adams | last post by:
Question about using delegates with a string paramater to return a ListViewItem object... All, I have a thread and I want this thread to post messages to the main GUI thread using a delegate....
2
by: Brad Wood | last post by:
I have a method that takes a ref string parameter. This is because it will be called in loops and most of the time it will not need to modify the paramater at all, so I preferred not to return a...
9
by: red floyd | last post by:
Anybody have any preferences as to declaring primitive type parameter as const, if the function body doesn't change it? e.g.: int f(int x) { return x+2; }
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
3
by: iu2 | last post by:
Hi all, I find myselft wondering about the recommended way to pass strings to functions: int func(string s); or int func(const char *s);
5
by: coolguyaroundyou | last post by:
Consider the following codes: class abc { int i; public: abc() : i(0) {} void func() { .....some code....} };
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...

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.