1

In c++ I have read that constructors make implicit calls to the operators new and delete when memory allocation is required.What is the meaning of the statement?

  • Is this the quote of the statement, or the paraphrased statement? Please provide the source of the statement. – Algirdas Preidžius Oct 18 '18 at 13:40
  • 3
    No constructors doesn't call `new` or `delete`. It's the opposite: `new` calls constructors and `delete` call destructors. – Some programmer dude Oct 18 '18 at 13:41
  • More or less the same question: https://stackoverflow.com/questions/1885849/difference-between-new-operator-and-operator-new – divinas Oct 18 '18 at 13:45
  • The above statement is written in Object oriented programming with c++ by E Balagurusamy pg 131. – Prabal Aggarwal Oct 18 '18 at 14:36
  • 1
    I don't mean to disparage Mr Balagurusamy, but I think your education will benefit if you go to our [curated book list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), pick a book to read from there and... *repurpose* the book you are currently reading. – StoryTeller - Unslander Monica Oct 18 '18 at 14:49

2 Answers2

3

What the book is talking about is there are classes that hide that they are doing dynamic allocation for you. Lets look at a simple string class. In the constructor that copy's a c-string it will "implicitly" call new to allocate storage for the string. You don't have to do that manually call new and give the string class a pointer of the right size, the constructor does that for you, so the author calls it an "implicit call"

That is all he is trying to get at. Constructors and destructors may call new and delete themselves to handle memory allocation, freeing you from having to do it. This is how most of the standard containers work. They allocate the storage they need and you never have to worry about it. This is a core part of RAII (Resource acquisition is initialization) and is a very useful and major part of proper C++ design.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

Basically, Implicit calling is like the behind the scenes work of the compiler. so C++ has many pre-made member functions available for classes.

So when your constructor for example calls 'new' or 'delete' these functions already know what to do and when to call themselves. You don't have to predefine them or manually call them, they're present at the service of your class constructors and destructors and know when to call themselves.

SeDak
  • 21
  • 1