Link to home
Start Free TrialLog in
Avatar of shayad
shayad

asked on

Default constructor

Hi,

Is it wrong to define a constructor (taking no arguments) in a class. The default constructor is generated by the compiler but, then it will just initialize the object. By placing some initialization code in the constructor we can initialize the object in a more appropriate manner. Is my conjecture correct ?

What errors can occur If i define a constructor (taking no arguments) and call it directly in an application.

shayad.
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

>> The default constructor is generated by the compiler

Not always.  The compiler will not even try to generate a default constructor if you write any procedures for the class.  Also it will not write a default constructor if any of its non-static data members or immediate base classes don't have default constructors.  So there are many times when the compiler will not generate a default constructor, and you will have to do so (if you want one).

>> What errors can occur If i define a constructor
>> (taking no arguments) and call it
>> directly in an application
You are not allowed to call constructors directly.  If you create an object, you can specify which constructor is tol be used when it is initialized, but the compiler will call the constructor for you after it creates the object. This is similar to calling a constructor, but with some very significant differences.