?
Solved

Default constructor

Posted on 1999-11-01
2
Medium Priority
?
193 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:shayad
2 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 300 total points
ID: 2176118
>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
 
LVL 22

Expert Comment

by:nietod
ID: 2176968
>> 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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question