Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Default Constructors (http://www.go4expert.com/forums/default-constructors-t755/)

sharmila 24Apr2006 18:22

Default Constructors
 
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 25Apr2006 09:49

Re: Default Constructors
 
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

coderzone 25Apr2006 10:34

Re: Default Constructors
 
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 25Apr2006 13:02

Re: Default Constructors
 
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 25Apr2006 20:43

Re: Default Constructors
 
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 26Apr2006 09:05

Re: Default Constructors
 
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 26Apr2006 09:57

Re: Default Constructors
 
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 26Apr2006 11:48

Re: Default Constructors
 
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


All times are GMT +5.5. The time now is 09:25.