I am studying C++ Primer, and it says "The default constructor is used automatically whenever an object is default or value initialized" on section 7.5.3. I've always thought a default constructor is called when an object is created without parameters. But based on the quote, if I initialize an obj sd:
Sales_data *sd = new Sales_data("ISBN: 5555");
then the constructor below is called:
Sales_data(const std::string &s): bookNo(s) {}
My question: Are all constructors defined in a class/struct default constructors? Thanks a lot!
1) No need to put the access specifier in line 2. All members of a struct are public by default.
2) A default constructor sets every member variable to 0. Just like what you did in line 4, with an overloaded constructor, set every member variable to 0.
Structs are default public. Classes are default private.
Constructors and destructors are mainly a classes thing, but structs do have them, the difference being that they are private by default in a class and are public by default in a struct.
Default constructors do not automatically set everything to zero.
No, not all constructors are default. If you define a constructor, you no longer get a default one. Once you define one, the default one is gone for good unless you put " = default".
You really should get in the habit of using a zero-argument constructor to initialize your variables.
Just to be sure I get it right...... if I call "Sales_data(const std::string &s): bookNo(s) {}" with an empty string "" as a parameter, I set sd to point to an empty string?
Yes. You can also have a bunch of different constructors, all taking different parameters. You have free reign over that.
You can define your own default constructor (takes no argument) or let the compiler make one for you (either by defining no constructor or by using "= default"). With a default constructor, regardless of who made it, you do not have to give parameters when creating it. You could define your own default constructor that initializes everything to '0' or whatever.
What you're really doing there is creating a new Sales_data structure and making it's variable "bookNo" be assigned to whatever string you send in.
Keep in mind that un-initialized variables are not good, and could lead to undefined behavior. My compiler (Visual Studio 2015) will fail to compile if I use a variable before I initialize it - that includes structs and classes.
Thank you for clarifying the concept,JayhawkZombie! But I am a bit confused about the two statements you made in your last 2 posts:
(1) If you define a constructor, you no longer get a default one. Once you define one, the default one is gone for good unless you put " = default".
(2) You could define your own default constructor that initializes everything to '0' or whatever.
Since the parameter list should be empty when I use "default" keyword (line 3), how do I define a default constructor that initialize values? Thank you in advance for your help.
If you use default, you don't write your own implementation. Keep in mind that "= default" came with C++11, so this will not be described in books that do not cover that standard.
"= default" signals the compiler to create the default constructor for you. Omitting it means that you will be forced to create your own if you leave the declaration in the class/struct definition.
1 2 3 4
class MyClass
{
MyClass() = default; //compiler will make it for us
};
1 2 3 4
class MyClass
{
MyClass(); //we have to provide the implementation ourselves now
};
You can also use "= default" for things other than a constructor as well.
Don't be confused by the keyword default and a default constructor.
A default constructor is simply a constructor that can be called without passing any arguments.
A defaulted constructor is a constructor that the compiler generates for you. To explicitly say you want a constructor to be defaulted you use the = default syntax. This only works for some standard types of constructors like the default and copy constructors. You can also use it with other special member functions like the copy and move assignment operators.