Reputation:
If I have a user defined default constructor in my Test class, and what operations will done by using the following statement:
Test *test = new Test; //there is no () after new Test
First, does the user default constructor means "constructor without Parameters"? for example:
class Test {
public:
Test() {
// do something here
}
}
so the new Test; means the complier will call the Test() constructor in class Test(); and perform the operations inside it, and allocate memory in heap for class Test object?
And what about the *test"? where is it ? in heap or stack? can anyone explain me? And what about
Test test = new Test();//with () this time
what kind of constructor will be called in this case?
Upvotes: 1
Views: 121
Reputation: 206526
Does the user default constructor means "constructor without Parameters"?
Yes, Default constructor means the constructor without any parameters. A constructor is a special & only member function for a class which has same name as the class.
The compiler generates a default constructor(constructor without any parameters) if your code needs one.
What does Test *test = new Test;
do?
It allocates memory on the heap of size = size of class Test and calls the default constructor Test()
.
*And what about the test"? where is it ? in heap or stack? can anyone explain me?
*test
is a pointer variable of the type Test
on the stack
which points to a dynamically allocated chunk of memory of the size of class Test on the Heap
Upvotes: 1
Reputation: 10558
A default constructor
means a constructor that can be called without arguments. It is used to initialize the parameters of any object of a class, not necessarily pointers to objects of that class. So,
Test t;
would call the default constructor Test::Test()
. Note that t
is an object and not pointer to object. Some languages also have default constructors that are automatically generated when explicit constructors are not defined and over-ridden when constructors are defined.
The new
operator in C++ allocates memory to the pointer from the heap and initializes it using the constructor.
HTH,
Sriram.
Upvotes: 0
Reputation: 2263
"Default constructor" means a constructor that can be called without parameters. It may have default parameters:
class Test {
public:
Test(int x = 42); // Still default constructor.
// Can be called as Test() and as Test(int);
};
The new
operator will call operator new
to allocate memory, and provided the allocation succeeds, then call one or more constructors. In this case it will call the default one to construct your object.
More than one constructor will be called if your object has a base class.
The Test*
itself, test
, will reside on the stack in this case.
Upvotes: 2
Reputation: 2778
Default constructor means the one which can be called without any parameters which includes constructors with parameters having all default values.
The statement Test *test = new Test
allocates the memory for object of Test on heap and calls the default constructor for it.
Upvotes: 1