J
jimmyi
Hi, friends, i've just encountered a problem on explicit copy
constructors, my friend was read that when a function returns a object
by value, the object can be created outside the function if it is
returned in the way "return-and-copy-construct", so one can prevent
temporary object being created once inside the function and again when
the function returns the same object, the code example can be this:
#include <iostream>
using namespace std;
struct foo {
foo() { cout << "contructor called.\n"; }
foo(const foo& f) { cout << "copy-constructor called.\n"; }
~foo() { cout << "destructor called.\n"; }
int i;
};
foo fun(const foo& f)
{
/*foo ff;
ff.i = f.i;
return ff;*/
return foo(f);
}
int main()
{
foo f(fun(foo()));
}
if return ff instead of foo(f), gcc makes no difference, but vc6 will
create another copy of parameter f when the funcion returns. talk
about this problem seems trivial because it relies on the fact that
compiler could take lot of optimization works behind the scenes, thus
different compilers may have different default optimization behavior
and the result thus differs.
but my friend tried the first aproach(i.e. return ff) and thought that
behavior seems inefficient(through the fact that vc6 doesn't make a
good guess and as a newbie), and thus he tried out a way to eliminate
the problem by what the phrase i was invented "return-and-copy-
construct"(i.e. return foo(f)), though this trivial problem is easy to
explain(i'll tell the him the fact later), but his question is now
become:
when the copy constructor was declared explicit, the code above just
can't be compiled.
and gcc 3.3.1 gives the following errors:
test.cpp: In function `foo fun(const foo&)':
test.cpp:13: error: no matching function for call to `foo::foo(foo)'
i was not very experienced and my learning on c++ was also in
progress, although searched many possible solutions and documents. the
answer seems still obscure, hope someone can give me a convictive
explanation about this, many thanks.
P.S.
i'm not the native english speaker and the words above may be
syntactically ill formed, parden for my bad english
constructors, my friend was read that when a function returns a object
by value, the object can be created outside the function if it is
returned in the way "return-and-copy-construct", so one can prevent
temporary object being created once inside the function and again when
the function returns the same object, the code example can be this:
#include <iostream>
using namespace std;
struct foo {
foo() { cout << "contructor called.\n"; }
foo(const foo& f) { cout << "copy-constructor called.\n"; }
~foo() { cout << "destructor called.\n"; }
int i;
};
foo fun(const foo& f)
{
/*foo ff;
ff.i = f.i;
return ff;*/
return foo(f);
}
int main()
{
foo f(fun(foo()));
}
if return ff instead of foo(f), gcc makes no difference, but vc6 will
create another copy of parameter f when the funcion returns. talk
about this problem seems trivial because it relies on the fact that
compiler could take lot of optimization works behind the scenes, thus
different compilers may have different default optimization behavior
and the result thus differs.
but my friend tried the first aproach(i.e. return ff) and thought that
behavior seems inefficient(through the fact that vc6 doesn't make a
good guess and as a newbie), and thus he tried out a way to eliminate
the problem by what the phrase i was invented "return-and-copy-
construct"(i.e. return foo(f)), though this trivial problem is easy to
explain(i'll tell the him the fact later), but his question is now
become:
when the copy constructor was declared explicit, the code above just
can't be compiled.
and gcc 3.3.1 gives the following errors:
test.cpp: In function `foo fun(const foo&)':
test.cpp:13: error: no matching function for call to `foo::foo(foo)'
i was not very experienced and my learning on c++ was also in
progress, although searched many possible solutions and documents. the
answer seems still obscure, hope someone can give me a convictive
explanation about this, many thanks.
P.S.
i'm not the native english speaker and the words above may be
syntactically ill formed, parden for my bad english