Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > On const reference arguments passing

Reply
Thread Tools

On const reference arguments passing

 
 
Giff
Guest
Posts: n/a
 
      04-16-2007
Hi,

I am trying to change my way of programming (I am still learning) , in
particular I am putting an effort in passing const ref to functions,
when possible.

When possible means (to me) when that function is not going to modify
the object I pass to it.

Many times though, I create an object, call a function (taking a non-
const ref) that modifies it somehow and then
need to use that object as parameter for a function that takes a const
ref, that will only read the object.

The compiler complains, since I am passing a non-const object to the
function and the only thing that I can do is to cast away the
constness, but is this the right way to go? It feels wrong...

Thanks for your hints.

/G

 
Reply With Quote
 
 
 
 
Giff
Guest
Posts: n/a
 
      04-16-2007
On 16 Apr, 16:12, "Giff" <(E-Mail Removed)> wrote:

Forget my post.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-16-2007
Giff wrote:
> I am trying to change my way of programming (I am still learning) , in
> particular I am putting an effort in passing const ref to functions,
> when possible.
>
> When possible means (to me) when that function is not going to modify
> the object I pass to it.


That's a good rule. Also, when you think of treading the object as
"a value", it may still be reasonable to pass by reference to const,
instead of passing by value.

> Many times though, I create an object, call a function (taking a non-
> const ref) that modifies it somehow and then
> need to use that object as parameter for a function that takes a const
> ref, that will only read the object.


That sounds reasonable.

> The compiler complains, since I am passing a non-const object to the
> function and the only thing that I can do is to cast away the
> constness, but is this the right way to go? It feels wrong...


That doesn't sound right. Could you please support this statement
with code? I can only see such behaviour of the compiler if the
situation is reversed -- calling a function expecting a ref to non-
const object from a function where the object is const (i.e. you
passed the object by reference to const):

class a {};
void foo(a& ra);
void bar(a const & ra) {
foo(ra); // error
}
int main() {
a object;
bar(object);
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Giff
Guest
Posts: n/a
 
      04-16-2007
On 16 Apr, 16:27, "Victor Bazarov" <(E-Mail Removed)> wrote:

> > The compiler complains, since I am passing a non-const object to the
> > function and the only thing that I can do is to cast away the
> > constness, but is this the right way to go? It feels wrong...

>
> That doesn't sound right.


I know, sorry for the post. I was doing something else wrong and I
have just been too impatient.
My apologies to the group, and thanks for your reply anyway.

 
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
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
non-const reference and const reference George2 C++ 10 12-17-2007 02:19 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments