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.
shayadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
>Is it wrong to define a constructor (taking no arguments) in a class.

No.

>Is my conjecture correct ?

Yes.

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

No errors provided you use it correctly.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
>> 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.