472,347 Members | 2,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,347 software developers and data experts.

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 1831
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Carlo v. Dango | last post by:
Hello all. I am in the need of wrapping certain objects at run-time. My initial approach was: import inspect, new def make_wrapper(obj,...
4
by: markscottwright | last post by:
Just for the hell of it, I've been going through the old Scheme-based textbook "Structure and Interpretation of Computer Programs" and seeing what...
2
by: Steve | last post by:
Hi Folks, Sorry for this stupid question, but how do you handle errors during class construction. In other words, if I have a class that loads a...
13
by: DaKoadMunky | last post by:
I recently came across some code in a template that default constructed an object of type T to pass to another function... SomeFunction(T()); ...
14
by: trying_to_learn | last post by:
i am on the chapter on copy construction in C++ in the code (see below), the author says if u pass the object by value as in HowMany h2 = f(h); ...
15
by: Jakob Bieling | last post by:
Hi, I am aware of the fact, that the order of construction of global objects is unspecified. But I am in a situation, where I need to guarantee,...
20
by: Merrill & Michele | last post by:
I am determining how best to call an already compiled c program from another. The following is a program that compiles and shows the information...
3
by: MKoool | last post by:
Hi everyone, I am doing several operations on lists and I am wondering if python has anything built in to get every member of several objects...
3
by: Paul | last post by:
Hello, and thanks for listening. I have a Stored Procedure as follows: -- PROCEDURE . -- Add the parameters for the stored procedure here ...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.