returning value from constructors

K

Kavya

I was reading a book Test Your C++ Skills by Yashwant Kanetkar. There
was a question in it

Ques: Why constructors do not have return values?
Ans :Constructors are called whenever an object is created. And there
can never exist a situation where we want to return a value at the time
of creation of an object.

I don't understand why author says that.
What if I want to check whether the object is successfully created. We
could have checked that by returning some value and finding it.
 
J

Jacek Dziedzic

Kavya said:
What if I want to check whether the object is successfully created. We
could have checked that by returning some value and finding it.

That's what you use exceptions for.

- J.
 
A

Alf P. Steinbach

* Kavya:
I was reading a book Test Your C++ Skills by Yashwant Kanetkar. There
was a question in it

Ques: Why constructors do not have return values?
Ans :Constructors are called whenever an object is created. And there
can never exist a situation where we want to return a value at the time
of creation of an object.

I don't understand why author says that.

Most probably because of a lack of imagination & experience.

What if I want to check whether the object is successfully created. We
could have checked that by returning some value and finding it.

Yes, but C++ is designed -- intentionally and/or perhaps just as a
consequence of Doing The Right Thing -- for using exceptions for this.
When a constructor fails, let it throw an exception. That way there
will never be any uninitialized, unusable objects around.

If you will, read the last sentence again.

The real reason why C++ constructors don't have return values, other
than the object created, is that that would force a much less convenient
and much less safe, not to mention much less efficient, syntax for
calling constructors. Consider:

std::cout << std::string( 40, ' ' ) << std::endl;

What if this std::string constructor returned a bool, say? Instead of
the single line above you'd have to do something like

{
std::string spaces; // Not initialized in this hypothetical C++.
if( !create( spaces, 40, ' ' ) )
{
std::runtime_error ex;
if( !create( ex, "Bah!" ) ) { std::terminate(); }
throw ex;
}
else
{
try
{
std::cout << spaces << std::endl;
destroy( spaces );
}
catch( std::exception& )
{
destroy( spaces );
throw;
}
}
}

Argh!
 
F

Frederick Gotham

Kavya:
What if I want to check whether the object is successfully created. We
could have checked that by returning some value and finding it.

Two Strategies:

(1) If construction fails, throw an exception.

class MyClass {
public:

MyClass(int const i)
{
if (42==i) throw -1;
}
};

(2) Check the state after construction:

MyClass obj;

if ( !obj.IsOpen() ) return -1;
 
D

Daniel T.

Kavya said:
I was reading a book Test Your C++ Skills by Yashwant Kanetkar. There
was a question in it

Ques: Why constructors do not have return values?
Ans :Constructors are called whenever an object is created. And there
can never exist a situation where we want to return a value at the time
of creation of an object.

I don't understand why author says that.
What if I want to check whether the object is successfully created. We
could have checked that by returning some value and finding it.

If a constructor returned a bool (true for success and false for failure
to create say,) then what would the following line of code mean?

MyClass myObject;

After the line, does 'myObject' exist or not?
 
J

Jim Langston

Kavya said:
I was reading a book Test Your C++ Skills by Yashwant Kanetkar. There
was a question in it

Ques: Why constructors do not have return values?
Ans :Constructors are called whenever an object is created. And there
can never exist a situation where we want to return a value at the time
of creation of an object.

I don't understand why author says that.
What if I want to check whether the object is successfully created. We
could have checked that by returning some value and finding it.

Okay, say you have a constructor that returns some val. So what does this
do?

MyClass MyInstance; // default constructor
MyClass = MyClass( 10 );

A constructor can be thought of as returning a value, the class itself.
Anythign else and how could you do code like that?
 
V

Victor Bazarov

Jim said:
[..]
A constructor can be thought of as returning a value, the class
itself. [..]

I think it's better to say that it returns *an instance* of the
class rather than "the class".

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top