473,399 Members | 3,401 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,399 software developers and data experts.

const in argument

hi:

When we declare something like:
func(const class a)

we protect the variable a from being modified during func...right? okay
first question: if a is passed by value, who care if it's modified??!!

now, what about we do
func(const class& a) or
func(const class* a)
then, in either case, is a begin protected? or a pointer that points to a?

thankx!
Jul 19 '05 #1
14 3035
>
But a is the pointer. What is being protected is the object that a points
(or refers) to. This is a very important consideration because the caller of func certainly is interested if calling func will modify a.


I mean of course.

if calling func will modify the object that a points (or refers) to.

john
Jul 19 '05 #2
"Matthew Monopole" <ma*******@hotmail.com> wrote in message
news:zk******************@newsread1.prod.itd.earth link.net...

When we declare something like:
func(const class a)

we protect the variable a from being modified during func...right?
What you wrote isn't legal C++, but I assume you mean something like this:

void func (const Foo a);
first question: if a is passed by value, who care if it's modified??!!
Probably no one. There isn't much point in making a parameter const if it's
a copy of the caller's argument.
now, what about we do
func(const class& a) or
func(const class* a)
then, in either case, is a begin protected? or a pointer that points to a?


In the first case, a is a reference to an object passed by the caller.
Here, the "const" means that you can't use a to modify that object.

In the second case, a is a pointer which holds a copy of an address passed
by the caller. Here "const" means that you can't modify the object to which
a points if you access it through a. a itself is not const; you can modify
its value if you want. Since a holds a copy of the caller's pointer, the
caller's pointer is not modified when you change a.

Regards,

Russell Hanneken
rh*******@pobox.com
Jul 19 '05 #3
>
But a is the pointer. What is being protected is the object that a points
(or refers) to. This is a very important consideration because the caller of func certainly is interested if calling func will modify a.


I mean of course.

if calling func will modify the object that a points (or refers) to.

john
Jul 19 '05 #4
I read somewhere that when overloading operator, it is best to use friend
and declare like this:
friend operator+(const class1&, const class2&);

Is there a reason why would one prefer friend over class1obj.operator+(const
class2&)? I used to thought it was for commutivity between classes, for
example, when we want class1obj+class2obj=class2obj+class1obj, but this
turns out to be not true. I still have to overload the operator twice with
the argument order reversed....

and (relating to earlier post) why do we (have to) use this const class&,
instead of just class? my guess is that you don't want to waste time copying
the object?

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bh************@ID-196037.news.uni-berlin.de...

But a is the pointer. What is being protected is the object that a points (or refers) to. This is a very important consideration because the
caller of
func certainly is interested if calling func will modify a.


I mean of course.

if calling func will modify the object that a points (or refers) to.

john

Jul 19 '05 #5
"Matthew Monopole" <ma*******@hotmail.com> wrote in message
news:zk******************@newsread1.prod.itd.earth link.net...

When we declare something like:
func(const class a)

we protect the variable a from being modified during func...right?
What you wrote isn't legal C++, but I assume you mean something like this:

void func (const Foo a);
first question: if a is passed by value, who care if it's modified??!!
Probably no one. There isn't much point in making a parameter const if it's
a copy of the caller's argument.
now, what about we do
func(const class& a) or
func(const class* a)
then, in either case, is a begin protected? or a pointer that points to a?


In the first case, a is a reference to an object passed by the caller.
Here, the "const" means that you can't use a to modify that object.

In the second case, a is a pointer which holds a copy of an address passed
by the caller. Here "const" means that you can't modify the object to which
a points if you access it through a. a itself is not const; you can modify
its value if you want. Since a holds a copy of the caller's pointer, the
caller's pointer is not modified when you change a.

Regards,

Russell Hanneken
rh*******@pobox.com
Jul 19 '05 #6
"Matthew Monopole" <ma*******@hotmail.com> wrote in message
news:zk******************@newsread1.prod.itd.earth link.net...
When we declare something like:
func(const class a) I assume you intend to use a type such as 'int' or 'MyStruct'
instead of 'class'.
we protect the variable a from being modified during func...right? okay
first question: if a is passed by value, who care if it's modified??!!
The point is: you protect the copy of the parameter used by func
from being modified within func. Which is just as useful as
using const with any other local variable declaration.

However, this const is NOT part of the function's signature -- and does
not belong to the function's interface.
So the style I would personally recommend is:

//file.h
void func(int a);

//file.cpp
void func(int const a)
{
....
}

See also the following post for info on what the standard says:
groups.google.com/groups?selm=3c6472fa%241%40news.swissonline.ch
now, what about we do
func(const class& a) or
'a' is passed by reference -- and the const protects it from
being changed.
func(const class* a)
then, in either case, is a begin protected? or a pointer that points to a?


f(const int* a) is equivalent to f(int const* a): the value being
pointed to is protected from any modifications. Which is
meaningful to the caller.

Note that one could use:
//file.h
void func(int const* a); // func does not modify *a

//file.cpp
void func(int const* const a)
{ // func does not modify *a, and 'a' itself is const here too
....
}

hth,
--
Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 19 '05 #7
I read somewhere that when overloading operator, it is best to use friend
and declare like this:
friend operator+(const class1&, const class2&);

Is there a reason why would one prefer friend over class1obj.operator+(const
class2&)? I used to thought it was for commutivity between classes, for
example, when we want class1obj+class2obj=class2obj+class1obj, but this
turns out to be not true. I still have to overload the operator twice with
the argument order reversed....

and (relating to earlier post) why do we (have to) use this const class&,
instead of just class? my guess is that you don't want to waste time copying
the object?

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bh************@ID-196037.news.uni-berlin.de...

But a is the pointer. What is being protected is the object that a points (or refers) to. This is a very important consideration because the
caller of
func certainly is interested if calling func will modify a.


I mean of course.

if calling func will modify the object that a points (or refers) to.

john

Jul 19 '05 #8
"Matthew Monopole" <ma*******@hotmail.com> wrote in message
news:10******************@newsread1.prod.itd.earth link.net...
Is there a reason why would one prefer friend over class1obj.operator+(const class2&)? I used to thought it was for commutivity between classes, for
example, when we want class1obj+class2obj=class2obj+class1obj, but this
turns out to be not true. I still have to overload the operator twice with
the argument order reversed....
NB: What does this have to do with the topic of this thread (const usage) ?

The point of using 'friend' is that it allows implicit conversions on
both sides of the operator.
For example, if you have a complex number class with a non-explicit
constructor such as:
complex(float f) : r(f), i(0) {}

And try to call it as follows:
void f(complex a, complex b, float f)
{
complex c0 = a+b; // ok anyway
complex c1 = a+f; // ok anyway
complex c2 = f+a; // only works if operator+ is a friend
}
and (relating to earlier post) why do we (have to) use this const class&,
instead of just class? my guess is that you don't want to waste time copying the object?


Yes: it helps prevent unnecessary object copies when copies are expensive.
hth
--
Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 19 '05 #9
"Matthew Monopole" <ma*******@hotmail.com> wrote in message
news:zk******************@newsread1.prod.itd.earth link.net...
When we declare something like:
func(const class a) I assume you intend to use a type such as 'int' or 'MyStruct'
instead of 'class'.
we protect the variable a from being modified during func...right? okay
first question: if a is passed by value, who care if it's modified??!!
The point is: you protect the copy of the parameter used by func
from being modified within func. Which is just as useful as
using const with any other local variable declaration.

However, this const is NOT part of the function's signature -- and does
not belong to the function's interface.
So the style I would personally recommend is:

//file.h
void func(int a);

//file.cpp
void func(int const a)
{
....
}

See also the following post for info on what the standard says:
groups.google.com/groups?selm=3c6472fa%241%40news.swissonline.ch
now, what about we do
func(const class& a) or
'a' is passed by reference -- and the const protects it from
being changed.
func(const class* a)
then, in either case, is a begin protected? or a pointer that points to a?


f(const int* a) is equivalent to f(int const* a): the value being
pointed to is protected from any modifications. Which is
meaningful to the caller.

Note that one could use:
//file.h
void func(int const* a); // func does not modify *a

//file.cpp
void func(int const* const a)
{ // func does not modify *a, and 'a' itself is const here too
....
}

hth,
--
Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 19 '05 #10

"Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote in message
news:3f********@news.swissonline.ch...
"Matthew Monopole" <ma*******@hotmail.com> wrote in message
news:10******************@newsread1.prod.itd.earth link.net...
Is there a reason why would one prefer friend over class1obj.operator+(const
class2&)? I used to thought it was for commutivity between classes, for
example, when we want class1obj+class2obj=class2obj+class1obj, but this
turns out to be not true. I still have to overload the operator twice with the argument order reversed....


NB: What does this have to do with the topic of this thread (const usage)

?
The point of using 'friend' is that it allows implicit conversions on
both sides of the operator.


Which also means in cases where you don't want an implicit conversion on the
left hand side, you should the non-friend form. This is usually the case
with assignment operators +=, *= etc.

john
Jul 19 '05 #11
"Matthew Monopole" <ma*******@hotmail.com> wrote in message
news:10******************@newsread1.prod.itd.earth link.net...
Is there a reason why would one prefer friend over class1obj.operator+(const class2&)? I used to thought it was for commutivity between classes, for
example, when we want class1obj+class2obj=class2obj+class1obj, but this
turns out to be not true. I still have to overload the operator twice with
the argument order reversed....
NB: What does this have to do with the topic of this thread (const usage) ?

The point of using 'friend' is that it allows implicit conversions on
both sides of the operator.
For example, if you have a complex number class with a non-explicit
constructor such as:
complex(float f) : r(f), i(0) {}

And try to call it as follows:
void f(complex a, complex b, float f)
{
complex c0 = a+b; // ok anyway
complex c1 = a+f; // ok anyway
complex c2 = f+a; // only works if operator+ is a friend
}
and (relating to earlier post) why do we (have to) use this const class&,
instead of just class? my guess is that you don't want to waste time copying the object?


Yes: it helps prevent unnecessary object copies when copies are expensive.
hth
--
Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 19 '05 #12
Matthew Monopole wrote:
I read somewhere that when overloading operator, it is best to use
friend and declare like this:
friend operator+(const class1&, const class2&);
Only make it a friend if it really needs to access private members.
Is there a reason why would one prefer friend over
class1obj.operator+(const class2&)? I used to thought it was for
commutivity between classes, for example, when we want
class1obj+class2obj=class2obj+class1obj, but this turns out to be not
true. I still have to overload the operator twice with the argument
order reversed....
But you can put both at the same place. If they were members, you would
need to put one into class1, the other one into class2.
and (relating to earlier post) why do we (have to) use this const
class&, instead of just class? my guess is that you don't want to
waste time copying the object?


Yes.

Jul 19 '05 #13

"Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote in message
news:3f********@news.swissonline.ch...
"Matthew Monopole" <ma*******@hotmail.com> wrote in message
news:10******************@newsread1.prod.itd.earth link.net...
Is there a reason why would one prefer friend over class1obj.operator+(const
class2&)? I used to thought it was for commutivity between classes, for
example, when we want class1obj+class2obj=class2obj+class1obj, but this
turns out to be not true. I still have to overload the operator twice with the argument order reversed....


NB: What does this have to do with the topic of this thread (const usage)

?
The point of using 'friend' is that it allows implicit conversions on
both sides of the operator.


Which also means in cases where you don't want an implicit conversion on the
left hand side, you should the non-friend form. This is usually the case
with assignment operators +=, *= etc.

john
Jul 19 '05 #14
Matthew Monopole wrote:
I read somewhere that when overloading operator, it is best to use
friend and declare like this:
friend operator+(const class1&, const class2&);
Only make it a friend if it really needs to access private members.
Is there a reason why would one prefer friend over
class1obj.operator+(const class2&)? I used to thought it was for
commutivity between classes, for example, when we want
class1obj+class2obj=class2obj+class1obj, but this turns out to be not
true. I still have to overload the operator twice with the argument
order reversed....
But you can put both at the same place. If they were members, you would
need to put one into class1, the other one into class2.
and (relating to earlier post) why do we (have to) use this const
class&, instead of just class? my guess is that you don't want to
waste time copying the object?


Yes.

Jul 19 '05 #15

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

Similar topics

3
by: Philippe Mesmeur | last post by:
J'ai eu une longue discussion hier au sujet des parametres de fonction const. La personne etait pour mettre "const" devant TOUS les parametres ne devant pas etre modifies. A mon avis, il faut...
7
by: Richard Cavell | last post by:
Hi, The point of using const on a parameter to a function should be to let your compiler know that the parameter shouldn't be modified during your program. This allows you to keep your code...
17
by: codeslinger | last post by:
What is the point of the construct in the subject line? Why the use of two const labels? Does that do something different than const void *x? And what would void * const x indicate? Thanks in...
8
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g....
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
8
by: Laurijssen | last post by:
What are the advantages of using const as often as possible in C? Does it help to declare integer function arguments as const? How about pointers and automatic const integers? const int...
5
by: amvoiepd | last post by:
Hi, My question is about how to use const properly. I have two examples describing my problem. First, let's say I have a linked list and from it I want to find some special node. I write the...
4
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
7
by: W Marsh | last post by:
Hi, Could anybody tell me wh the parameter "T val" is not marked const in this Stroustrup code, considering that val is not modified and not non- const methods called? template<class C,...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.