473,408 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

C++ What is the use of a implict default constructor provided by the comiler ?

9
In C++ if no user defined constructor is provided for a class the compiler automatically provides a implicit default constructor.

An implicit default constructor is equivalent to a default constructor with no parameters and no body ?

So what is the use of a implicit default constructor provided by the compiler, if it is not going to do anything ?
Sep 21 '14 #1
3 1187
weaknessforcats
9,208 Expert Mod 8TB
The default constructor calls the constructor for each member of your class. So it does something. Like here:

Expand|Select|Wrap|Line Numbers
  1. MyClass obj;
obj will have each member initialized by calling its constructor.
Sep 21 '14 #2
You say each member is initialized by calling its constructor. OK, initialized to what? To whatever the constructor feels like? LOL. I mean if no user defined constructor is provided then where are the initializing values coming from?
Sep 22 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
The compiler knows how to initialize the built-in types but if your class has user-defined type as a member and there is no default constructor for that type, then your compile fails on this code:

Expand|Select|Wrap|Line Numbers
  1. MyClass obj;
  2.  
Initialization is a big deal in C++ since major initialization errors in C slip by the C compiler. One of the C++ goals was to plug these errors at compile time rather than when the program was running.

C does not do member-wise initialization. It doesn't do any initialization at all. There is reliance on the mem--- functions which can easily screw up the stack.
Sep 22 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Satish Sangapu | last post by:
I am reading "Inside the C++ Object Model" by Stanley Lippman. It is a very nice book about the internals of C++. Anyway, I am confused about one thing. He states that a default constructor is not...
3
by: Aire | last post by:
Will defining a copy constructor also make the default constructor not available anymore, until a default constructor is explicitly provided? Thanks!
17
by: highli | last post by:
When a non-default constructor provided in a class, the default constructor is not available anymore. In what cases shall a default constructor be defined explicitly? Specifically, in the...
6
by: Gunnar G | last post by:
If I don't define a default constructor for my class "Foo", I still get one from the compiler. But if I define a constructor that takes an argument, I don't get the default constructor, why? I...
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
3
by: swengtoo | last post by:
In his book "More Effective C++", Scott Meyer suggests in "Item 4" to "Avoid gratuitous default constructors". To summarize his claims against default constructors for "the right classes" he...
10
by: JosephLee | last post by:
In Inside C++ object Model, Lippman said there are four cases in which compile will sythesize a default constructor to initialize the member variables if the constructor is absent: 1. there is a...
3
by: Rahul | last post by:
Hi Everyone, I have the following code and the compiler complains that there isn't any default constructor available, class C { private: C() {
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.