Get premium membership and access questions with answers, video lessons as well as revision papers.

The copy constructor is also involked when a function generates the temporary object that is used as the function's return value. With this in...

      

The copy constructor is also involked when a function generates the temporary object that is used as the function's return value. With this in mind, consider the following output:
constructing nomally
constructing normally
constructing copy

This output was created by the following program. Explain why, and describe precisely what is occuring.
#include
using namespace std;
class myclass {
public:
myclass();
myclass(const myclass &o);
myclas f();
};
// Normal constructor
myclass::myclass()
{
cout << "Constructing normally\n";
}
// Copy constructor
myclass::myclass(const myclass &ocout << "Constructing copy\n";
}
// Return an object.
myclass myclass::f()
{
myclass temp;
return temp;
}
int main()
{
myclass obj;
obj = obj.return 0;
}

  

Answers


Davis
The obj and temp objects are constructed nomally. However, when temp is returned by f(), a temporary object is made, and it is this temorary object that generates the call to the copy constructor.
Githiari answered the question on May 29, 2018 at 18:02


Next: Explain the purpose of copy constructor and how it differs from a normal constructor.
Previous: Create a function called mag() using the following prototype that raises num to the order of magnitude specified by order.(use C++ language) void mag(long &num, long...

View More Computer Science Questions and Answers | Return to Questions Index


Learn High School English on YouTube

Related Questions