473,412 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

constructors

Sir,

Why don't we have return type for constructor and destructors?
Dec 18 '07 #1
3 1224
gpraghuram
1,275 Expert 1GB
Sir,

Why don't we have return type for constructor and destructors?

Constructors are not called by us directly and the compiler is the one which takes care of calling it and the use of constructor is to initialize the member variables of the object.

Raghuram
Dec 18 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Constructors are not called by us directly and the compiler is the one which takes care of calling it and the use of constructor is to initialize the member variables of the object.
Nope. That's not the reason. Constructors are called by us directly in many cases.

The real reason is that constrcutors are used to initialize the data members of the object. If, for some reason, an initialization fails, and you had a return type on the constructor, then you could return, never fix the problem, and go off with an improperly initialized object.

The absence of the return type forces you to throw exceptions when errors occur in a constructor. This stops the creation process of the object and forces you to deal with the problem.

Beginner C++ jockeys with a C background have been known to put an error code data member in the class. Then when their constructor croaks, they put the reason for the error in the error code data member in the object and return. Then they call some goofball method, like GetLastError(), to retrieve the error code. Now they can test the error code just like they did in C. Apologies to Microsoft, but this was a stupid thing to have done.
Dec 18 '07 #3
gpraghuram
1,275 Expert 1GB
Nope. That's not the reason. Constructors are called by us directly in many cases.

The real reason is that constrcutors are used to initialize the data members of the object. If, for some reason, an initialization fails, and you had a return type on the constructor, then you could return, never fix the problem, and go off with an improperly initialized object.

The absence of the return type forces you to throw exceptions when errors occur in a constructor. This stops the creation process of the object and forces you to deal with the problem.

Beginner C++ jockeys with a C background have been known to put an error code data member in the class. Then when their constructor croaks, they put the reason for the error in the error code data member in the object and return. Then they call some goofball method, like GetLastError(), to retrieve the error code. Now they can test the error code just like they did in C. Apologies to Microsoft, but this was a stupid thing to have done.

Thanks for your very good explanation.
Thanks
Raghuram
Dec 19 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
6
by: Stephen Martinelli | last post by:
thanks for the help...just one more question.... can a class have more then two parameterized constructors?..i would like to be able to instanciate the class with a different number of...
4
by: Sathyaish | last post by:
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides: (1) public constructors; and (2) parameterized constructors And I understand...
10
by: John | last post by:
Trying to find out what is essential / optional, I made an extremely simple Class and Module combination to add two numbers. (see below) It appears that an empty constructor is needed n order to...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
22
by: Peter Morris [Droopy eyes software] | last post by:
Look at these two classes public class Test { public readonly string Name; public Test(string name)
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.