Default constructors...
-
Hi, May i know what are default constructors generated in c++ when an object is created... and what happens if i declare my own constructor without declaring the default constructor.. pls correct me if im wrong..
By default C++ generates a default constructor and a copy constructor for a class. If you define any of your own constructors, no other constructor will be generated by the compiler. The auto generated default constructor will initialize all class data members to 0. The auto generated copy constructor will do a bit by bit copy of all data members from one instance to the other.
«_Superman_» I love work. It gives me something to do between weekends.
-
Hi, May i know what are default constructors generated in c++ when an object is created... and what happens if i declare my own constructor without declaring the default constructor.. pls correct me if im wrong..
In addition to above points from superman, i will say always declare and define your own default constructors when your class is having pointer members. Along with default constructor, make your own copy constructor and operator = in that case. It is always good to write your own default constructor so that you can initialize your member variable in default constructor.
Величие не Бога может быть недооценена.
-
By default C++ generates a default constructor and a copy constructor for a class. If you define any of your own constructors, no other constructor will be generated by the compiler. The auto generated default constructor will initialize all class data members to 0. The auto generated copy constructor will do a bit by bit copy of all data members from one instance to the other.
«_Superman_» I love work. It gives me something to do between weekends.
«_Superman_» wrote:
The auto generated default constructor will initialize all class data members to 0.
Are You sure? I think it may be behavior of specific compiler You use.
«_Superman_» wrote:
The auto generated copy constructor will do a bit by bit copy of all data members from one instance to the other.
It's true rather for members of built-in types. For members of user defined types, straightway, is used method of constructing member objects with the help of their constructors.
-
By default C++ generates a default constructor and a copy constructor for a class. If you define any of your own constructors, no other constructor will be generated by the compiler. The auto generated default constructor will initialize all class data members to 0. The auto generated copy constructor will do a bit by bit copy of all data members from one instance to the other.
«_Superman_» I love work. It gives me something to do between weekends.
«_Superman_» wrote:
The auto generated default constructor will initialize all class data members to 0.
This is incorrect. If your class has built-in data types (int, char, etc.), the default behavior is to leave them uninitialized, just like local variables.
--Mike-- Dunder-Mifflin, this is Pam.