Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
One of the main motto of cpp is to avoid uninitialized variables. Then what is the use of default constructor that compiler provides as it doesn't initialize variables.
The implicitly generated default constructor calls the default constructors of all members and base classes. They may or may not be implicitly generated (meaning, some member, or member's member, etc, may have a non-implicitly generated default constructor, one that actually does something).
There are some circumstances in which a default constructor is required. For example
MyClass arrayOfObjects[10];
Here the default constructor is called even if then you are going to assign new values to objects in the array. Or a derived class ctor which doesn't explicitly call a parent constructor. Or even a simple declaration of a variable:
MyClass x; // calls default constructor
...
x = ..;
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
class Base
{
public:
~Base() {}
private:
int val;
};
Base base; // a global variable
Look, the destructor does nothing, just same as the default destructor provided by c++ complier. But the destructor is still also a non-trivial destructor accroding to the post What is a non-trivial destructor in C++?
I know, each RULE from the standard must be strict.
But, by the upper codes I pasted, the user-defined destructor does nothing really! Why it is also non-trivial? I don't understand....
Is there any magic I don't konw?
Having the rule absolutely prohibit a definition gives a meaning to providing an empty one: it specifies that its instances must not be forgotten in an array that provides their storage. What the destructor does, if anything, can be considered an implementation detail that might change in future versions. It also avoids a change in meaning based on whether the “empty” destructor is defined in the class definition; if it isn’t, it can even be binary-compatible to change it to do something.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
My question is on Default Constructors in C++.
After passing the above two objects separately, I found that
(a) A obj1, was detected as Default Constructor, and the constructor defining it was executed.
(b) A obj1(), was not detected as Default Constructor. It matched with none of the constructors.
In both the cases, there is no argument passed. Then why is that only (a) is set for Default Constructor and not the second, i.e (b).
Leta there be a class named A. What is the difference between passing the following two objects: (a) A obj1 and (b) A obj1()?
The difference is that A obj1; declares an object obj1 of type A. While A obj1(); declares a function obj1 which takes no arguments and returns an A - it does not create an object.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Suppose we declare A to be of type Type:
Type A;
Question: is the Type constructor for A called at this point? Or is it only after we initialize A that a constructor is called?
is the Type constructor for A called at this point? Or is it only after we initialize A that a constructor is called?
You are initializing A here, whether you explicitly provided a value for that process or not.
There is no opportunity to initialise A "later"; you can only assign to it later. (Early C texts talk about "initialising" numeric values long after declaration, but that's a different language, a different century, and a different set of descriptive idioms.)
So, yes, the constructor is called as soon as the object's lifetime begins. That's either right there and then, on that line, or if a class member then of course it's when the encapsulating class is being initialised and it's this member's turn.
Of course you could have proven this with a simple std::cout pair.
Yes, it is called immediately. When you assign it a value, the assignment operator is called instead.
The only exception is this:
Type a = value;
Here some compiler with some settings may relax it and call constructor directly with the value as parameter. But it still needs to be part of declaration.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am fairly new to the language, and I can't seem to find a good explanation on constructors.
When I don't create a constructor, according to many resources, a default constructor is created, which doesn't do anything. However, is there something that the constructor does behind the scenes that instantiates an object?
A comparison between the default constructor and my own defined constructors would help me understand this.
Thanks a ton in advance!
~novice
The constructor initializes the variables(fields) of a class. The default constructor initializes to default values. Example, string to "", integers to zero, doubles to 0.0, boolean to false an so on. When you create a constructor you're customizing the variables initialization.
A constructor is essentially the conditions called upon the object being created. If you want to input an int, for example, into the initialization of the object, you would create a constructor that takes "int x" in the parentheses, which is then referenced within the constructor statements.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
C++ will create copy constructor and copy assignment operator for class. My questions is why it is creating two member function ? What is the significance and what is the difference by copying the object by copy constructor and copy assignment operator? Thank you in advance.
Copy assignment and copy construction do different things. Copy assignment has to take a fully constructed object and change it, while copy construction has to take a non-fully constructed object and do that initial construction.
For example copy assignment on a class that manages a resource has to ensure that its old resource is properly disposed of after it has taken ownership of the 'copied' resource, whereas the copy constructor doesn't have any previous resource to deal with.
If you have pointers as data members in your class, and if they are directly getting copied to another object, more than one object will access that pointer memory (unintentionally). To avoid that we can override copy constructor/assignment operator.
If you do not override these two functions, compiler copies bit by bit to another object.