Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > use of copy constructor with default arguments

Reply
Thread Tools

use of copy constructor with default arguments

 
 
Andrey Vul
Guest
Posts: n/a
 
      10-19-2009
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?
 
Reply With Quote
 
 
 
 
Andrey Vul
Guest
Posts: n/a
 
      10-19-2009
On Oct 19, 5:27*pm, Andrey Vul <(E-Mail Removed)> 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>)

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-19-2009
* 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
 
Reply With Quote
 
Andrey Vul
Guest
Posts: n/a
 
      10-20-2009
On Oct 19, 6:51*pm, "Alf P. Steinbach" <(E-Mail Removed)> 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?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-20-2009
* Andrey Vul:
> On Oct 19, 6:51 pm, "Alf P. Steinbach" <(E-Mail Removed)> 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
 
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
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
function default arguments from other arguments tutmann C++ 4 10-17-2006 08:00 PM
Difference between default arguments and keyword arguments Edward Diener Python 14 04-05-2004 11:26 PM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments