473,564 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1885
On Sep 21, 5:15 am, "Jim Langston" <tazmas...@rock etmail.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...@rock etmail.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 objektorientier ter Datenverarbeitu ng
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
1914
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 board. Since this function does not change anything in the class I declared it as const. To count all pieces of a given color the functions iterates...
4
2393
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, but allowing iteration through the data? I tryed returning a const vector but the compiler does not like the idea of my using an iterator to look...
1
3898
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 = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/"; I think the idea at play here is whether the pointer is constant or not ?
2
1626
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. This is my code fragment: ListViewItem lview = m_form.Invoke( m_form.m_DelegateAddItem, new object {"hello"} );
2
1277
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 newly allocated string object. I need to pass items of an array list (can't use specialized collection in compact framework) to this method. I...
9
4004
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
11645
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const char *>(p)); ----------------------------------------------------------------------- ---------------------------------------- ...
3
15676
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
2636
by: coolguyaroundyou | last post by:
Consider the following codes: class abc { int i; public: abc() : i(0) {} void func() { .....some code....} };
0
7665
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7642
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.