M
Martijn van Buul
Hi,
I stumbled across a strange oddity regarding copy constructors, and I was
hoping someone could explain where I'm going wrong. I tried searching google,
and looking through documentation, but couldn't find anything (or I'm blind).
I combined my test cases in http://www.stack.nl/~martijnb/constructortest.cpp
but will summarize below.
Consider a class CBar. Constructor, copy constructor and destructor have
all been implemented. As expected,
CBar a;
CBar *b = new CBar(a);
results in a call to the default constructor and the copy constructor.
However:
CBar *b = new CBar(CBar())
does *not*. Instead, it only calls the default constructor. Even more
interesting, the line above *doesn't compile* if I make CBar's copy
constructor private.
Can anyone explain me why GCC insists the copy constructor exists, but
doesn't bother calling it afterwards? I'm using GCC 4.2.1.
Kind regards,
Martijn
I stumbled across a strange oddity regarding copy constructors, and I was
hoping someone could explain where I'm going wrong. I tried searching google,
and looking through documentation, but couldn't find anything (or I'm blind).
I combined my test cases in http://www.stack.nl/~martijnb/constructortest.cpp
but will summarize below.
Consider a class CBar. Constructor, copy constructor and destructor have
all been implemented. As expected,
CBar a;
CBar *b = new CBar(a);
results in a call to the default constructor and the copy constructor.
However:
CBar *b = new CBar(CBar())
does *not*. Instead, it only calls the default constructor. Even more
interesting, the line above *doesn't compile* if I make CBar's copy
constructor private.
Can anyone explain me why GCC insists the copy constructor exists, but
doesn't bother calling it afterwards? I'm using GCC 4.2.1.
Kind regards,
Martijn