469,626 Members | 979 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

Construction of function return value

class Foo
{
public :
explicit Foo(int i) : m_int(i) {}

private :
int m_int;
};

Foo f(int i)
{
return i;
}

Foo g(int i);
{
return Foo(i);
}

int main()
{
f(2); // is a Foo ever constructed?
}

Will a Foo be constructed by the return statement in f() even if the
return value is ignored in the calling function or does the
constructor have to be explicitly called as in g()?
Jul 22 '05 #1
7 1675
"Michael Klatt" <md*****@ou.edu> wrote in message
news:2c**************************@posting.google.c om...
class Foo
{
public :
explicit Foo(int i) : m_int(i) {}

private :
int m_int;
};

Foo f(int i)
{
return i;
If the above compiles, it's a compiler bug. Are you using VC++6.0? This is a
known bug in that compiler.
}

Foo g(int i);
{
return Foo(i);
This is the right way of returning an object with an 'explicit' constructor.
}

int main()
{
f(2); // is a Foo ever constructed?
}

Will a Foo be constructed by the return statement in f() even if the
return value is ignored in the calling function or does the
constructor have to be explicitly called as in g()?


I am not sure about the answer to this one. (?) I believe though, if the
constructor and/or the destructor of Foo has side effects, I don't think the
compiler has the right to elide the construction.

Ali
Jul 22 '05 #2
Michael Klatt wrote in
news:2c**************************@posting.google.c om in comp.lang.c++:
class Foo
{
public :
explicit Foo(int i) : m_int(i) {}

private :
int m_int;
};

Foo f(int i)
{
Did you try compiling, you should get a invalid conversion or
similar error here, Foo only has an explict ctor.
return i;
}

Foo g(int i);
{
return Foo(i);
}

int main()
{
f(2); // is a Foo ever constructed?
}

Will a Foo be constructed by the return statement in f()
It won't compile.
even if the
return value is ignored in the calling function
Return values are constructed by the the function that is
returning them, wether or not the caller uses the value
doesn't matter.
or does the
constructor have to be explicitly called as in g()?


If Foo::Foo( int ) wasn't explicit then f() would compile
and both it and g() would be equivalent.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
Michael Klatt wrote:

[snip]
cat foo.cc class Foo {
private :
int m_int;
public:
explicit Foo(int i): m_int(i) { }
};

Foo f(int i) {
return i;
}

Foo g(int i) {
return Foo(i);
}

int main(int argc, char* argv[]) {
f(2); // Is a Foo ever constructed?
return 0;
}
g++ -Wall -ansi -pedantic -o foo foo.cc foo.cc: In function `Foo f(int)':
foo.cc:9: error: \
conversion from `int' to non-scalar type `Foo' requested
Will a Foo be constructed by the return statement in f()
No.
even if the return value is ignored in the calling function
or does the constructor have to be explicitly called as in g()?


Yes because it is an *explicit* constructor.
Jul 22 '05 #4
>Return values are constructed by the the function that is
returning them, wether or not the caller uses the value
doesn't matter.


Given the following...

class SomeClass
{
public:

SomeClass()
{
}

SomeClass& operator=(const SomeClass&)
{
return *this;
}

private:

SomeClass(const SomeClass&)
{
}
};

SomeClass SomeFunction()
{
SomeClass sc;
return sc; //Produces error because of inaccessible copy constructor
}

int main()
{
SomeClass sc;
sc = SomeFunction(); //Produces error because of inaccessible copy
constructor
}

I can see why an error is produced in SomeFunction() because it is attempting
to invoke a copy constructor that is inacessible.

Why though does the code in function main() result in an error? I don't see
how it is that the copy constructor is involved within that function.

Thanks

Jul 22 '05 #5
da*********@aol.com (DaKoadMunky) wrote in
news:20***************************@mb-m26.aol.com:
Given the following...

class SomeClass
{
public:

SomeClass()
{
}

SomeClass& operator=(const SomeClass&)
{
return *this;
}

private:

SomeClass(const SomeClass&)
{
}
};

SomeClass SomeFunction()
{
SomeClass sc;
return sc; //Produces error because of inaccessible copy
constructor
}

int main()
{
SomeClass sc;
sc = SomeFunction(); //Produces error because of inaccessible copy
constructor
}

I can see why an error is produced in SomeFunction() because it is
attempting to invoke a copy constructor that is inacessible.

Why though does the code in function main() result in an error? I
don't see how it is that the copy constructor is involved within that
function.


The statement

SomeClass sc

creates a fully constructed object. It is not like Java where it would
create only a reference to one that has yet to exist. Then the statement

sc = SomeFunction()

reassigns sc the new one returned by SomeFunction.

Gregg
Jul 22 '05 #6
DaKoadMunky wrote in news:20***************************@mb-m26.aol.com
in comp.lang.c++:
I can see why an error is produced in SomeFunction() because it is
attempting to invoke a copy constructor that is inacessible.

Why though does the code in function main() result in an error? I
don't see how it is that the copy constructor is involved within that
function.


The function return's an rvalue, however the assignment operator takes
a constant lvalue (const &). This conversion from rvalue to constant
lvalue symanticly requires the creation of a temporary (which can
be an lvalue).

Its the construction of the temporary that requires an accesable
copy-constructor.

Implementation's are allowed to eleminate the creation of the
temporary (as an optimisation), but they aren't allowed to drop
the accesable copy-constructor requirement.

Hence the error message in main().

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #7
Why though does the code in function main() result in an error? I don't see how it is that the copy constructor is involved within that function.

Thanks


Whenever you return an object from a function by value, as
in:

#include <iostream>

std::string Blah()
{
return std::string("Hello!");
}

Then the copy constructor needs to be public, as the
function must have the choice of copying the object before
returning it, (which most won't do).

-JKop

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Carlo v. Dango | last post: by
4 posts views Thread by markscottwright | last post: by
2 posts views Thread by Steve | last post: by
13 posts views Thread by DaKoadMunky | last post: by
14 posts views Thread by trying_to_learn | last post: by
15 posts views Thread by Jakob Bieling | last post: by
20 posts views Thread by Merrill & Michele | last post: by
3 posts views Thread by MKoool | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.