Default Constructors

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

  1. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Hi all,
    This time I have a doubt in C++ default constructors.In a site

    http://publib.boulder.ibm.com/infoc...bm.vacpp7a.doc/language/ref/clrc15cplr376.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.
     
  2. rai_gandalf

    rai_gandalf New Member

    Joined:
    Nov 4, 2005
    Messages:
    46
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Final Year Comp Engg
    Location:
    Mumbai
    Home Page:
    http://mindpuncture.blogspot.com
    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: Apr 25, 2006
  3. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    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

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    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

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    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

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice