use of copy constructor with default arguments

Discussion in 'C++' started by Andrey Vul, Oct 19, 2009.

  1. Andrey Vul

    Andrey Vul Guest

    Given the following:

    class Bar {};
    class Foo {
    Bar *parent;
    Foo(Bar *p, const Foo& f = 0) {
    parent = p;
    if (f != 0) {
    /* do something with f */
    }
    }
    };

    Why does the compiler die while Foo& =0 is used, but works when Foo*
    =0 is used?
    Andrey Vul, Oct 19, 2009
    #1
    1. Advertising

  2. Andrey Vul

    Andrey Vul Guest

    On Oct 19, 5:27 pm, Andrey Vul <> wrote:

    > Why does the compiler die while Foo& =0 is used, but works when Foo*
    > =0 is used?


    Update: the same error condition is given by the compiler as when a
    recursive template is unbound (i.e. the famous template factorial
    without the factorial<0>)
    Andrey Vul, Oct 19, 2009
    #2
    1. Advertising

  3. * Andrey Vul:
    > Given the following:
    >
    > class Bar {};
    > class Foo {
    > Bar *parent;
    > Foo(Bar *p, const Foo& f = 0) {
    > parent = p;
    > if (f != 0) {
    > /* do something with f */
    > }
    > }
    > };
    >
    > Why does the compiler die while Foo& =0 is used, but works when Foo*
    > =0 is used?


    What you wrote is equivalent to

    Foo( Bar* p, Foo const& f = Foo( 0 ) )

    which if it was valid would be equivalent to

    Foo( Bar* p, Foo const& f = Foo( 0, Foo( 0 ) ) )

    which if it was valid would be equivalent to

    Foo( Bar* p, Foo const& f = Foo( 0, Foo( 0, Foo( 0 ) ) ) )

    and so on.


    C:\test> gnuc x.cpp
    x.cpp: In constructor `Foo::Foo(Bar*, const Foo&)':
    x.cpp:6: error: no match for 'operator!=' in 'f != 0'

    g++ compiler has a bug, it should have reported error on the argument declaration.


    C:\test> msvc x.cpp
    x.cpp
    x.cpp(4) : fatal error C1202: recursive type or function dependency context too
    complex

    C:\test> _

    Visual C++ compiler also has a bug.

    "Doctor, it hurts when I do *this*!" - "Then simply stop doing that."


    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Oct 19, 2009
    #3
  4. Andrey Vul

    Andrey Vul Guest

    On Oct 19, 6:51 pm, "Alf P. Steinbach" <> wrote:
    > * Andrey Vul:
    >
    > > Given the following:

    >
    > > class Bar {};
    > > class Foo {
    > > Bar *parent;
    > > Foo(Bar *p, const Foo& f = 0) {
    > > parent = p;
    > >  if (f != 0) {
    > > /* do something with f */
    > > }
    > > }
    > > };

    >
    > > Why does the compiler die while Foo& =0 is used, but works when Foo*
    > > =0 is used?

    >
    > What you wrote is equivalent to
    >
    >    Foo( Bar* p, Foo const& f = Foo( 0 ) )
    >
    > which if it was valid would be equivalent to
    >
    >    Foo( Bar* p, Foo const& f = Foo( 0, Foo( 0 ) ) )
    >
    > which if it was valid would be equivalent to
    >
    >    Foo( Bar* p, Foo const& f = Foo( 0, Foo( 0, Foo( 0 ) ) ) )
    >
    > and so on.
    >


    So null [pointer] / pure virtual semantic defaults don't work with
    references?
    Or do they only work when the first argument to the constructor is not
    a pointer?
    Or does the C++ standard specify nasal demons?
    Andrey Vul, Oct 20, 2009
    #4
  5. * Andrey Vul:
    > On Oct 19, 6:51 pm, "Alf P. Steinbach" <> wrote:
    >> * Andrey Vul:
    >>
    >>> Given the following:
    >>> class Bar {};
    >>> class Foo {
    >>> Bar *parent;
    >>> Foo(Bar *p, const Foo& f = 0) {
    >>> parent = p;
    >>> if (f != 0) {
    >>> /* do something with f */
    >>> }
    >>> }
    >>> };
    >>> Why does the compiler die while Foo& =0 is used, but works when Foo*
    >>> =0 is used?

    >> What you wrote is equivalent to
    >>
    >> Foo( Bar* p, Foo const& f = Foo( 0 ) )
    >>
    >> which if it was valid would be equivalent to
    >>
    >> Foo( Bar* p, Foo const& f = Foo( 0, Foo( 0 ) ) )
    >>
    >> which if it was valid would be equivalent to
    >>
    >> Foo( Bar* p, Foo const& f = Foo( 0, Foo( 0, Foo( 0 ) ) ) )
    >>
    >> and so on.
    >>

    >
    > So null [pointer] / pure virtual semantic defaults don't work with
    > references?


    Formally there is no nullvalue for a reference.

    That's much of the point of references.

    Re "pure virtual semantics defaults" I think you're referring to the notation "=
    0". There's no such thing for references. It can be meaningful and accepted, but
    for a T& reference it then means to construct a T object with 0 as constructor
    argument, T(0), since a reference must refer to an object.


    > Or do they only work when the first argument to the constructor is not
    > a pointer?
    > Or does the C++ standard specify nasal demons?


    If you happen to dereference a nullpointer you get what in practice would be a
    null-reference, except that you've then already crossed the border to Undefined
    Behavior land (nasal demons), yes. :)


    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Oct 20, 2009
    #5
    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. Aire
    Replies:
    3
    Views:
    454
    Mike Wahler
    Jan 25, 2004
  2. Edward Diener
    Replies:
    14
    Views:
    4,884
    Josiah Carlson
    Apr 6, 2004
  3. tutmann
    Replies:
    4
    Views:
    427
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,197
  5. cinsk
    Replies:
    35
    Views:
    2,559
    James Kanze
    Oct 11, 2010
Loading...

Share This Page