[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Subject: [LI] Some C++ doubts



Hi Sridhar,
This is very simple to explain. Normally in a function definition you precede the declaration with a int/float/string /char/void etc, this declares the type that the function will be returning after finishing its assigned work. But a constructor is a special function which is not supposed to return anything - to avoid confusion you are not even allowed to use "void" for its declaration. In the first case you are trying to return something from a function which is not supposed to return anything- hence you will get compile time error.. In second case it is working fine because you are making a normal call to the constructor and then returning another valid argument (temp in your case). Coming back to your case - a constructor is not supposed to return anything is also a very fundamental aspect of OOP. A constructor is called whenever an instance of an object needs to be instantiated - so unless the object has been created succesfully it has got no meaning to expect it to return something. And if it failed to creat an instance of an object successfully then also your code will not work as the required object has not been created. But sometimes you need to know the status of outcome of constructor so here is what you can do- include a statement like `cout<<"ALL OK" ' in your constructor code(this will be displayed only if the object has been instantiated succesfully).Include a "throw-catch" code for handling the appropriate exception in your constructor code. Now this code will be executed only in the event of constructor failing to instantiate an object. Your main code execution cannot proceed because the required object has not been constructed succesfully so the `exception handling ' gives you the flexibilty to recover from such hopeless situations.
HTH
Pankaj Mathur
Bangkok

___________________________________________________________________
>  1)An error is encountered when constructor is involved in return
>statement. e.g. following statement is not allowed
>    return(Vector ftemp(temp));
 >But following thing works.
>    Vector ftemp(temp);
>    return(temp);
>    Shridhar
______________________________________________________________________
______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

--------------------------------------------------------------------
The Linux India Mailing List Archives are now available.  Please search
the archive at http://lists.linux-india.org/ before posting your question
to avoid repetition and save bandwidth.