1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Default Constructors

Discussion in 'C++' started by sharmila, Apr 24, 2006.

  1. sharmila

    sharmila New Member

    Hi all,
    This time I have a doubt in C++ default constructors.In a site


    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.

  2. rai_gandalf

    rai_gandalf New 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.

    Last edited: Apr 25, 2006
  3. coderzone

    coderzone Super Moderator

    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.
  4. sharmila

    sharmila New Member

    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.
  5. shabbir

    shabbir Administrator Staff Member

    Probably its loaded but you have not choosen the correct project. I am just assuming as I dont have the idea about the error.
  6. sharmila

    sharmila New Member

    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.
  7. shabbir

    shabbir Administrator Staff Member

    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.
  8. sharmila

    sharmila New Member

    I tried sample Hello program.Only one cout statement.Before it compiles it is prompting an error
    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

Share This Page