terminator schrieb:
Quote:
On Jul 26, 8:20 pm, Olaf <o...@mdcc.dewrote:
Quote:
>Hi,
>>
>here the following test code
>>
>#include <iostream>
>>
>using namespace std;
>>
>struct Foo {
> Foo() { cout << " Foo()\n"; }
> ~Foo() { cout << "~Foo()\n"; }
> void set() const { cout << "Foo::set()\n"; }
>>
>};
>>
>struct Bar {
> void set(const Foo& foo) const { foo.set(); }
>>
>};
>>
>int main() {
> Bar b;
> b.set( Foo() );
>>
>}
>>
>shows:
>>
> Foo()
> Foo()
>Foo::set()
>~Foo()
>~Foo()
>>
>Well, obviously the copy ctor from temporary is called. Can I avoid this
>by using a different way?
>
*This is obviously wrong. Since you have not prepared a copy-ctor,any
copy construction would be silent(no-output).*
Yes, you are right. I did verify the snippet again. No idea what I did
before (probably for test purpose there was a Foo created on stack).
Sorry for confusion.
Anyway, here a more real problem of me:
#include <iostream>
#include <boost/utility.hpp>
using namespace std;
struct Foo : public boost::noncopyable {
Foo() { cout << " Foo()\n"; }
~Foo() { cout << "~Foo()\n"; }
//Foo( const Foo& ) { cout << " Foo(const Foo&)\n"; }
void set() const { cout << "Foo::set()\n"; }
};
struct Bar {
void set(const Foo& foo) const { foo.set(); }
};
int main()
{
Bar b;
b.set( Foo() );
}
$ LANG=en g++ cctor.cpp -o cctor && ./cctor
/usr/include/boost/noncopyable.hpp: In copy constructor 'Foo::Foo(const
Foo&)':
/usr/include/boost/noncopyable.hpp:27: error:
'boost::noncopyable_::noncopyable::noncopyable(con st
boost::noncopyable_::noncopyable&)' is private
cctor.cpp:6: error: within this context
cctor.cpp: In function 'int main()':
cctor.cpp:21: note: synthesized method 'Foo::Foo(const Foo&)' first
required here
Background more precise: Foo can have std::list<Tand function pointer
private data.
I can verify where the copy ctor is involved. Using the own (out
commented) Foo(const Foo&) did compile and run fine, but I'm not shure
about side effects here.
Thanks,
Olaf