3

I am new to c++ and would like to know what is the difference between calling a constructor with pointer and without pointer. By without pointer I mean with an instance of the class.

For example:

#include <iostream> 
using namespace std;

class abc {
    abc() {
        cout << "constructor";
    }

    ~abc() {
        cout << "destructor";
    }
};

int main() {
    abc a;
    return 0;
}

// VS
int main() {
   abc* a = new abc();
   delete a;
   return 0;
}

Is the only difference that it's dynamically called and otherwise both are same, e.g. produce the same result?

Timo
  • 2,212
  • 2
  • 25
  • 46
  • 5
    Well `abc a();` doesn't call the constructor, it declares a function called `a` which takes no arguments and returns an `abc`. See [most vexing parse](https://en.wikipedia.org/wiki/Most_vexing_parse). – TartanLlama Jul 13 '15 at 09:41
  • 3
    possible duplicate of [Most vexing parse: why doesn't A a(()); work?](http://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work) – danielschemmel Jul 13 '15 at 09:43
  • @gha.st I don't think he is trying to find out why abc a() is not working – Othman Benchekroun Jul 13 '15 at 09:48
  • 1
    Possible duplicate, or if nothing else you might be interested in reading:[Why should I use a pointer rather than the object itself](http://stackoverflow.com/questions/22146094/why-should-i-use-a-pointer-rather-than-the-object-itself) – Tas Jul 13 '15 at 09:52

3 Answers3

4

abc a; allocates a on the stack, calling its default constructor. The destructor will be called when a goes out of scope.

abc* a = new abc(); allocates memory for an abc object on the heap, calls its default constructor and returns a pointer to the object. Remember to call delete a; (which will call the destructor) else you'll leak memory.

abc a(); is a function prototype for a function called a that returns an abc, taking no parameters.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 3
    Remark: "stack" and "heap" aren't actually in the standard (except for stack unwinding and `std::stack` and so on). See section 3.7. Better call them "automatic/dynamic storage duration" as per 3.7.3 and 3.7.4, which explains the terms even better than "stack" and "heap". – Zeta Jul 13 '15 at 09:50
1
class {
....
}abc;

abc a;
abc *a = new abc();

In both situation the class constructor is called because you are instantiating an object. But there are a few other subtle differences.

  1. abc a; Here the object is allocated on the stack (if you declare it in your main or other function), OR in the .data section if you declare it globally. Either case the memory is allocated statically; there is no memory management to worry about.
  2. abc *a = new abc(); Now you are allocating memory dynamically for your object and you need to take care of the memory management yourself, i.e. call delete() or have e memory leak in your program.

But there is one other difference between the two ways of instantiating your object: in the second example you are also calling operator new. Which can be overloaded to give you a different behavior. Also because the second call involves operator new, you can use placement new and allocate your object in a memory location of your choosing - you cannot do this with the first example.

Pandrei
  • 4,843
  • 3
  • 27
  • 44
0

The dynamic allocation can be broken in two pieces:

abc *a; // doesn't call the constructor
a=new abc(); //calls the constructor

You will find this type of allocation more helpful when you learn about the run-time polymorphism(i.e. a pointer to a class can hold an object of another class).

NB: Your program,as written here,won't compile,because the constructor is private.

flaviumanica
  • 195
  • 1
  • 4
  • 14