1

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?

  • There is no "heap" or "stack" in C++. – Alexandre C. May 05 '11 at 09:43
  • You might want to look at http://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new (the first answer), because what happens depends whether the default constructor is user-defined or compiler-generated. In your example it is user-defined, so answers to this question are not necessarily applicable to classes in which it isn't. In particular if `Test` has a compiler-generated default constructor, then `new Test()` calls it in C++98, but in C++03 it value-initializes instead. – Steve Jessop May 05 '11 at 12:04

4 Answers4

2

"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.

Lstor
  • 2,265
  • 17
  • 25
1

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.

mukeshkumar
  • 2,698
  • 3
  • 19
  • 20
1

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

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Where the definition of "needs one" in the first paragraph is closer to *if you did not provide any constructor at all and try to default construct an element*. +1 for the rest of it even if that particular sentence is probably not the best description. – David Rodríguez - dribeas May 05 '11 at 09:22
0

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.

Sriram
  • 10,298
  • 21
  • 83
  • 136