defult reference parameters

P

puzzlecracker

I seem to get an error for this kind of code; apparently c++ prohibits
default params for nonconst refs types.

what are my alternatives without having to declare it const and then
const_cast it?
thanks

#include<iostream>
class A{

};

class B:public A{}

;
void f (A & a= A()){


}
int main(){



}
 
V

Victor Bazarov

puzzlecracker said:
I seem to get an error for this kind of code; apparently c++ prohibits
default params for nonconst refs types.

Nope. It's binding a non-const ref to a temporary what's prohibited.
what are my alternatives without having to declare it const and then
const_cast it?

Why const_cast it? If you intend to change it, how can you pass
temporaries? What does it mean to change a temporary?
thanks

#include<iostream>
class A{

};

class B:public A{}

;

Add:

A a_default;
void f (A & a= A()){

Change to

void f (A & a= a_default){

But be aware that if you change 'a' inside 'f', you may be changing the
'a_default', so next time it's called with the default, it will be
a different "default". You might want to look into declaring your
argument a reference to const:

void f (A const& a = A()){

or overloading 'f':

void f () { // no argument whatsoever
A a; // your "default"
f(a);
}
}
int main(){



}

V
 
A

Alf P. Steinbach

* puzzlecracker:
I seem to get an error for this kind of code; apparently c++ prohibits
default params for nonconst refs types.

No, it doesn't.

what are my alternatives without having to declare it const and then
const_cast it?
thanks

#include<iostream>
class A{

};

class B:public A{}

;
Formatting.


void f (A & a= A()){

You cannot bind a non-const reference to a temporary.
 
P

puzzlecracker

void f (A & a= A()){
You cannot bind a non-const reference to a temporary.

I know that, thanks. I am asking for a design technique used to achieve
this behaviour type. It seems as I want to couple plymorphism with
overloading....
I believe Mayers has a great discussion on this theme.
 
P

puzzlecracker

void f (A & a= A()){
You cannot bind a non-const reference to a temporary.

I know that, thanks. I am asking for a design technique used to achieve
this behaviour type. It seems as I want to couple plymorphism with
overloading....
I believe Mayers has a great discussion on this theme.
 
P

puzzlecracker

puzzlecracker said:
didnt mean spam it.

I should clarify my question a little bit.

I am passing a callback that I am intending to change, but for certain
services I just want to use a defualt callback that I dont really care
about.. that's why this issues came about.

thanks
 
A

Andre Kostur

didnt mean spam it.

I should clarify my question a little bit.

I am passing a callback that I am intending to change, but for certain
services I just want to use a defualt callback that I dont really care
about.. that's why this issues came about.

How about a singleton object somewhere that implements all of these "no-
op" callbacks, instead of having to instantiate a new one every time this
method is called with a default parameter?
 
I

Ivan Vecerina

:I seem to get an error for this kind of code; apparently c++
prohibits
: default params for nonconst refs types.
:
: what are my alternatives without having to declare it const and then
: const_cast it?
....
: void f (A & a= A()){

A relatively common idiom for optionally passing output parameters is
to pass them by pointer.
Which would be:
void f( A* a = 00 );
Some will also prefer not providing a default value for the parameter
(requiring that a NULL be explicitly passed as a last parameter):
void f( A* a );

Whether or not you like this idiom is a matter of style IMO.

Ivan
 
X

xuatla

Victor said:
Nope. It's binding a non-const ref to a temporary what's prohibited.



Why const_cast it? If you intend to change it, how can you pass
temporaries? What does it mean to change a temporary?



Add:

A a_default;



Change to

void f (A & a= a_default){
I encountered the same problem before.
For the way you suggested above, it won't work.
error: invalid use of member 'B:a_default'.

But be aware that if you change 'a' inside 'f', you may be changing the
'a_default', so next time it's called with the default, it will be
a different "default". You might want to look into declaring your
for this one, you may add a line to change 'a_default' to default
value in the end of 'f'.
argument a reference to const:

void f (A const& a = A()){

or overloading 'f':

void f () { // no argument whatsoever
A a; // your "default"
f(a);
}


V

-X
 
V

Victor Bazarov

xuatla said:
I encountered the same problem before.
For the way you suggested above, it won't work.
error: invalid use of member 'B:a_default'.

What are you talking about? How is 'B' involved here?
for this one, you may add a line to change 'a_default' to default
value in the end of 'f'.

This is simply not worth it, trust me.

V
 
K

Kai-Uwe Bux

puzzlecracker said:
I seem to get an error for this kind of code; apparently c++ prohibits
default params for nonconst refs types.

what are my alternatives without having to declare it const and then
const_cast it?
thanks

#include<iostream>
class A{

};

class B:public A{}

;
void f (A & a= A()){


}
int main(){



}

You could use

template < typename T >
T & lvalue_cast ( const T & ref ) {
return( const_cast< T & >( ref ) );
}


and then write

#include<iostream>
class A{

};

class B:public A{}

;

void f (A & a= lvalue_cast( A() ) ){


}
int main(){



}
 
G

Greg

puzzlecracker said:
didnt mean spam it.

I should clarify my question a little bit.

I am passing a callback that I am intending to change, but for certain
services I just want to use a defualt callback that I dont really care
about.. that's why this issues came about.

thanks

Well why not pass the callback by pointer? A NULL callback pointer
would instruct the routine to invoke the default callback routine.
Pointer parameters are useful in function declarations to identify
parameters whose values are optional or for which default values
exists.

Greg
 
B

benben

Well why not pass the callback by pointer? A NULL callback pointer
would instruct the routine to invoke the default callback routine.
Pointer parameters are useful in function declarations to identify
parameters whose values are optional or for which default values
exists.

Greg

Or, as I prefer, pass it with boost::function template.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top