Default Constructors

sharmila's Avatar, Join Date: Mar 2006
Contributor
Hi all,
This time I have a doubt in C++ default constructors.In a site

http://publib.boulder.ibm.com/infoce...c15cplr376.htm

I read about default constructors.In that it is menctioned that

No default constructor is created for a class that has any constant or reference type members.

I did not understand this statement.i.e if no default constructor is created and also no user-defined constructors then what happens.
Can anybody analyse this one for me.
Thanks in advance.

Regards,
sharmila.
rai_gandalf's Avatar, Join Date: Nov 2005
Go4Expert Member
There is a small clairification on this.

The statement should be : "No compiler generated (or implicit) default constructor is created for a class that has any constant or reference type members."

What it means is, that for a class having Constant type of members, the Compiler doesn't create an implicit default constructor of its own, like it does otherwise. You must explicitly mention the default constructor for it to compile & work.

Just try out a simple Class A with constant private members say 'x' & 'y', with a routine 'display( )' & WITHOUT a User-defined default constructor & then try compiling.

U will recieve an error of the sort :
'A::x' in Class without constructors
'A::y' in Class without constructors



Now, just compile the same, by changing the constant types of 'x' & 'y' to variable type. And, u will be able to compile it & run it.

Hope that's clear enough.


Ciao,
Rajiv

Last edited by rai_gandalf; 25Apr2006 at 09:51..
coderzone's Avatar, Join Date: Jul 2004
Team Leader
The reason there isn't any default constructor provided for the const and reference type variables is you need to initialize the constant variables before the object is created and that can be done in the initilization list of the constructors and so you need to be having a custom constructor and default one cannot be used.
sharmila's Avatar, Join Date: Mar 2006
Contributor
Thank you Rajiv and Coderzone.Noe I came to understand why user-defined constructors are necessary when there are constants.Thank you very much.
I tried that one in vc++.it is giving an error about cl.exe.may vc++ is not loaded properly.After installing again I will try.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by sharmila
Thank you Rajiv and Coderzone.Noe I came to understand why user-defined constructors are necessary when there are constants.Thank you very much.
I tried that one in vc++.it is giving an error about cl.exe.may vc++ is not loaded properly.After installing again I will try.
Probably its loaded but you have not choosen the correct project. I am just assuming as I dont have the idea about the error.
sharmila's Avatar, Join Date: Mar 2006
Contributor
Here I did not choosen any project.I just opened a c/c++ source file and wrote the code there.When I compiled it creates a default workspace and project.Every time we will do like that only(for test programs).At that time it will not give any error.Now my system is changed.In this system it is showing error.I too don't know much about this.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
In that case just try to compile some other very simple program. I guess if you have the country information changed the error can also come.
sharmila's Avatar, Join Date: Mar 2006
Contributor
I tried sample Hello program.Only one cout statement.Before it compiles it is prompting an error
EESWT.DLL
The requested file could not be loaded.Please Re-Install Visual C++.

when I click ok,it is compiled with an error message
Error spawning cl.exe