defult reference parameters

Discussion in 'C++' started by puzzlecracker, Sep 23, 2005.

  1. 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(){



    }
     
    puzzlecracker, Sep 23, 2005
    #1
    1. Advertising

  2. puzzlecracker wrote:
    > 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
     
    Victor Bazarov, Sep 23, 2005
    #2
    1. Advertising

  3. * 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.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Sep 23, 2005
    #3
  4. > 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.
     
    puzzlecracker, Sep 23, 2005
    #4
  5. > 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.
     
    puzzlecracker, Sep 23, 2005
    #5
  6. thx
     
    puzzlecracker, Sep 23, 2005
    #6
  7. puzzlecracker wrote:
    > thx

    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
     
    puzzlecracker, Sep 23, 2005
    #7
  8. puzzlecracker

    Andre Kostur Guest

    "puzzlecracker" <> wrote in
    news::

    >
    > puzzlecracker wrote:
    >> thx

    > 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?
     
    Andre Kostur, Sep 23, 2005
    #8
  9. "puzzlecracker" <> wrote in message
    news:...
    :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
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact
    form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Sep 24, 2005
    #9
  10. puzzlecracker

    xuatla Guest

    Victor Bazarov wrote:
    > puzzlecracker wrote:
    >
    >> 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){

    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);
    > }
    >
    >> }
    >> int main(){
    >> }
    >>

    >
    > V


    -X
     
    xuatla, Sep 24, 2005
    #10
  11. xuatla wrote:
    > Victor Bazarov wrote:
    >> puzzlecracker wrote:
    >>
    >>> 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){

    > 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?

    >> 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'.


    This is simply not worth it, trust me.

    V
     
    Victor Bazarov, Sep 24, 2005
    #11
  12. puzzlecracker

    Kai-Uwe Bux Guest

    puzzlecracker wrote:

    > 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(){



    }
     
    Kai-Uwe Bux, Sep 24, 2005
    #12
  13. puzzlecracker

    Greg Guest

    puzzlecracker wrote:
    > puzzlecracker wrote:
    > > thx

    > 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
     
    Greg, Sep 24, 2005
    #13
  14. puzzlecracker

    benben Guest

    > 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
     
    benben, Sep 24, 2005
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonck van der Kogel
    Replies:
    2
    Views:
    987
    Jonck van der Kogel
    May 27, 2004
  2. puzzlecracker

    defult constructor hiding

    puzzlecracker, Jan 25, 2005, in forum: C++
    Replies:
    1
    Views:
    409
    Rolf Magnus
    Jan 25, 2005
  3. Jason
    Replies:
    2
    Views:
    514
    Jonathan Mcdougall
    May 13, 2006
  4. kito
    Replies:
    2
    Views:
    417
  5. Florian Loitsch
    Replies:
    11
    Views:
    253
    Michael Winter
    Mar 15, 2005
Loading...

Share This Page