Can anyone explain me in simple language and better if with an example why constructor don't have return types :!:

Recommended Answers

All 10 Replies

Probably because the are not explicitely called in your code, so what good would it do to return a value -- nobody is going to be listening.

The constructor is only called when you instantiate an object. There is no way to catch the return value of the constructor.

// This doesn't make sense!!
int x = CMyClass SomeObject;

>Can anyone explain me in simple language and better if with an
>example why constructor don't have return types
Here's an idea. Show us how it could be done without breaking all kinds of existing code, and you'll answer your own question.

Thanx alot i guess it actually doesn't make any sense

I was searching net and came across one answer and it said having return types of constructor means taking address of constructor(which is illegal)...can u explain me this....

sorry for asking such queries (i am bit new to C++)

...can u explain me this....

Nope. I don't know what taking the address of a function has to do with returning a value. constructors don't return values because c++ standards and language say they don't. ;)

Another option -- don't put any code in the constructor that might cause it to fail. M$ MFC is good at that -- first instantiate the object and then call its Create() method. The constructor only initializes class data to some default value (normally 0). Then the Create() method initializes all the rest and either returns a value or throws and exception.

Yes, one could use MFC's 2-stage construction. However, my personal preference is still to to use the constructor to create the object, because I'm used to the RAII paradigm, and my own wrappers for the Windows API use it.

since every function should return a type so do consttructors(they return the type of the class ).but being the special member function according to the previliges provided by the developer we can't access that type
( i think so.) Becoz
class_name cls_var=class_name();
works.
i know that it may create a temporery object and then copy that in memory space in cls_varand then destroy the temporery.
But another aspect may be that it has a internally usable type(constructor type).

Can anyone explain me in simple language and better if with an example why constructor don't have return types :!:

constructors are meant to initialize your data members in your classes. They dont return any type because that was how they were built and programmed to work. Constructors dont even take a void type...thats the beauty of it....the set values to your data members and ensure proper initialization so that your program doesnt have garabge values and such

constructor do have return type a class object is getting memory this is the return type of a constructor

commented: zombie spammer -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.